Convert a List to a Tuple in Python
To convert a list to a tuple in Python, use the built-in tuple()
function. This function takes an iterable, such as a list, and returns a tuple containing the same elements in the same order.
Examples
1. Converting a List to a Tuple Using tuple()
Function
The simplest way to convert a list to a tuple is by passing the list to the tuple()
function.
# Creating a list
my_list = [1, 2, 3, 4, 5]
# Converting list to tuple
my_tuple = tuple(my_list)
# Printing the result
print("Original List:", my_list)
print("Converted Tuple:", my_tuple)
Explanation:
Here, we have a list named my_list
containing five integer elements. The function tuple(my_list)
is used to convert the list into a tuple and store it in the variable my_tuple
. The original list remains unchanged.
Output:
Original List: [1, 2, 3, 4, 5]
Converted Tuple: (1, 2, 3, 4, 5)
2. Converting a List of Strings to a Tuple
A list containing string elements can also be converted into a tuple using the same tuple()
function.
# Creating a list of strings
fruits = ["apple", "banana", "cherry"]
# Converting list to tuple
fruit_tuple = tuple(fruits)
# Printing the result
print("Original List:", fruits)
print("Converted Tuple:", fruit_tuple)
Explanation:
Here, the list fruits
contains three string elements. The function tuple(fruits)
converts the list into a tuple, storing it in fruit_tuple
. The elements in the tuple remain in the same order as they were in the list.
Output:
Original List: ['apple', 'banana', 'cherry']
Converted Tuple: ('apple', 'banana', 'cherry')
3. Converting a Nested List to a Tuple
Python allows lists within lists (nested lists) to be converted into tuples as well.
# Creating a nested list
nested_list = [[1, 2], [3, 4], [5, 6]]
# Converting nested list to tuple
nested_tuple = tuple(nested_list)
# Printing the result
print("Original Nested List:", nested_list)
print("Converted Nested Tuple:", nested_tuple)
Explanation:
The variable nested_list
contains sublists as elements. The tuple(nested_list)
function converts it into a tuple where each sublist remains unchanged as a list inside the tuple.
Output:
Original Nested List: [[1, 2], [3, 4], [5, 6]]
Converted Nested Tuple: ([1, 2], [3, 4], [5, 6])
4. Converting a List to a Tuple Using *
Operator
Python’s unpacking operator *
can also be used to create a tuple from a list.
# Creating a list
numbers = [10, 20, 30, 40]
# Using the * operator to create a tuple
number_tuple = (*numbers,)
# Printing the result
print("Original List:", numbers)
print("Converted Tuple:", number_tuple)
Explanation:
Here, the list numbers
is unpacked using the *
operator and enclosed in parentheses ()
to create a tuple. The trailing comma ,
ensures that the result is interpreted as a tuple.
Output:
Original List: [10, 20, 30, 40]
Converted Tuple: (10, 20, 30, 40)
Conclusion
Converting a list to a tuple in Python can be done in multiple ways:
- Using
tuple()
function: The simplest and most common method. - Converting lists of strings, numbers, or nested lists: Works in the same way as regular lists.
- Using the
*
operator: Unpacks list elements into a tuple.