If you want a data structure that won’t be modified easily, you can opt for a tuple. But if your current data is a list, you must convert it into a tuple.
The easiest and most efficient way to convert a list into a tuple is to use the built-in tuple() Constructor, which accepts an iterable(list) as its argument and returns a new tuple containing the elements of that iterable(list).
main_list = [11, 18, 19, 21, 46] print(main_list) # Output: [11, 18, 19, 21, 46] print(type(main_list)) # Output: <class 'list'> main_tuple = tuple(main_list) print(main_tuple) # Output: (11, 18, 19, 21, 46) print(type(main_tuple)) # Output: <class 'tuple'>
Once the tuple is created, you cannot modify it like in the list.
Empty list to tuple
If you try to convert an empty list to a tuple, it will return an empty tuple.
main_list = [] print(main_list) # Output: [] print(type(main_list)) # Output: <class 'list'> main_tuple = tuple(main_list) print(main_tuple) # Output: () print(type(main_tuple)) # Output: <class 'tuple'>
Nested list
If you apply the basic tuple() conversion to a nested list, the inner elements will remain lists, while the outer list will become a tuple.
nested_list = [[1, 2], [3, 4]] print(nested_list) # Output: [[1, 2], [3, 4]] print(type(nested_list)) # Output: <class 'list'> main_tuple = tuple(nested_list) print(main_tuple) # Output: ([1, 2], [3, 4]) print(type(main_tuple)) # Output: <class 'tuple'>
The above output shows that we did not get a tuple of tuples but a tuple of lists.
If we want to make the inner lists tuples, too, we need to use processed approach.
nested_list = [[1, 2], [3, 4]] print(nested_list) # Output: [[1, 2], [3, 4]] print(type(nested_list)) # Output: <class 'list'> def deep_list_to_tuple(lst): return tuple(deep_list_to_tuple(x) if isinstance(x, list) else x for x in lst) deep_tuple = deep_list_to_tuple(nested_list) print(deep_tuple) # Output: ((1, 2), (3, 4)) print(type(deep_tuple)) # Output: <class 'tuple'>
Here are three alternate ways to convert a list to a tuple:
- Using the unpacking operator(*)
- Using map() and lambda
- Using the processed tuple approach
Alternate way 1: Using the Unpacking Operator(*)
You can use the unpacking operator (*) to unpack a list and then repack it into a tuple.
This method is short and works well for small lists.
main_list = [11, 18, 19, 21, 46] print(main_list) # Output: [11, 18, 19, 21, 46] print(type(main_list)) # Output: <class 'list'> main_tuple = (*main_list, ) print(main_tuple) # Output: (11, 18, 19, 21, 46) print(type(main_tuple)) # Output: <class 'tuple'>
Alternate way 2: Using map() and lambda
The map() function applies the lambda function to each list element, returning each element as it is.
Then, tuple() converts the iterable returned by map() into a tuple.
main_list = [11, 18, 19, 21, 46] print(main_list) # Output: [11, 18, 19, 21, 46] print(type(main_list)) # Output: <class 'list'> main_tuple = tuple(map(lambda x: x, main_list)) print(main_tuple) # Output: <class 'list'> print(type(main_tuple)) # Output: <class 'tuple'>
Alternate way 3: Processed tuple approach
The processed approach mainly modifies elements while converting a list to a tuple.
main_list = [1, 2, 3, 4] print(main_list) # Output: [1, 2, 3, 4] print(type(main_list)) # Output: <class 'list'> processed_tuple = tuple(x * 2 for x in main_list) print(processed_tuple) # Output: (2, 4, 6, 8) print(type(processed_tuple)) # Output: <class 'tuple'>
The above output is a converted tuple and doubles each element.
That’s it!