Python | Convert List Into Tuple

In Python, data structures like “List” and “Tuple” store several elements/items in a single variable. List and tuple store multiple data type values inside it, such as integer, float, string, etc. List values are mutable and can be modified easily, while tuples are immutable and cannot be modified later in the program.

This write-up will show you how to convert list values into tuples in Python. The following topics are discussed with examples in this guide:

So let’s get started!

How to Convert a List Into a Tuple in Python?

To convert a list into a tuple in Python, various built-in functions such as “tuple()”, “for loop”, and “unpacking list operator” are utilized. Before directly jumping into methods, let’s look at the difference between the list and the tuple value.

ListTuple
The List value is mutable (list can be modified)The Tuple value is Immutable (tuple can not be modified)
The memory consumption in the list is more than tuple.The memory consumption in the tuple is less than a list.
There is a frequent occurrence of errorNo such type of error occurs
List iteration consumes a lot of time.Tuple iteration is faster than list
Lists are initialized inside the square bracket “[ ]”.A tuple is initialized inside the parentheses “( )”.
How to install Kali Linux in Window...

First, let’s look at the first method given below:

Method 1: Convert List Into Tuple Using “tuple()” Function

In the example given below, the “tuple()” function is used to typecast the list value into a tuple.

Code:

List_value = [1, 2, 3, 4]
Tuple_value = tuple(List_value)
print(Tuple_value)
print(type(Tuple_value))

In the above code:

  • A list is initialized with different values.
  • The function named “tuple()” takes the list variable as an input and converts the given list into a tuple.
  • To check the data type of a tuple, the ” type() ” function is utilized in the program.

Output:

This output confirms that the list value is typecast into a tuple.

Method 2: Convert List Into Tuple Using “for” Loop With “tuple()” Function

In the example given below, the “tuple()” function is also used with the combination of “for loop” to typecast the list value into a tuple.

Code:

List_value = [4, 5, 9, 8]
Tuple_value = tuple(item for item in List_value)
print(Tuple_value)
print(type(Tuple_value))

In the above code:

  • The list value is initialized.
  • The “tuple()” function is used along with “for loop” to iterate over the items of the list variable named “List_value” and returns the tuple value.
  • To check the data type of a final result, the ” type() ” function is utilized in the program.

Output:

This output confirms that the list value is typecast into a tuple using the “tuple()” function.

Method 3: Convert List Into Tuple Using (*List, ) or (Unpacking List)

In the example given below, the list value is converted into a tuple using the unpacks statement, i.e. “(*Lits_value, )”. The list element is unpacked inside the parenthesis and converted into tuples.

Code:

List_value=['Alex Cary', 18, 'Boy', 'Students']
Tuple_value= (*List_value,)
print(Tuple_value)
print(type(Tuple_value))

In the above code:

  • The list value is initialized with string and integer values.
  • The statement “(*List_value, )” is used to unpack the list value inside the parentheses. The unpacking value will convert into a tuple. The presence of a single comma “,” is mandatory to unpack the list inside the tuple literal.
  • The final output generated is of “tuple” data type.

Output:

As shown above, the list value has been converted to a tuple value.

That’s all from this Python Tutorial!

Conclusion

To convert the list into a tuple, the “tuple()” function, “for loop”, and “unpacking list operator (*List, ) ” with a single comma are used in Python. The inbuilt “tuple()” function typecast the list value into a tuple by taking the list variable as an argument. The “for loop” can be used with the combination of the “tuple()” function to convert the list value by iterating over it into a tuple. The list elements are unpacked inside the parentheses with an unpacking operator and a single comma. This Guide presented a detailed overview of converting the list into a tuple with multiple examples.