Python | Convert a list into a tuple
Given a list, write a Python program to convert the given list into a tuple.
Examples:
Input : [1, 2, 3, 4] Output : (1, 2, 3, 4) Input : ['a', 'b', 'c'] Output : ('a', 'b', 'c')
Approach #1 : Using tuple(list_name).
Typecasting to tuple can be done by simply using tuple(list_name).
# Python3 program to convert a
# list into a tuple
def convert(list):
return tuple(list)
# Driver function
list = [1, 2, 3, 4]
print(convert(list))
|
(1, 2, 3, 4)
Approach #2 :
A small variation to the above approach is to use a loop inside tuple() .
# Python3 program to convert a
# list into a tuple
def convert(list):
return tuple(i for i in list)
# Driver function
list = [1, 2, 3, 4]
print(convert(list))
|
Approach #3 : Using (*list, )
This essentially unpacks the list l inside a tuple literal which is created due to the presence of the single comma (, ). This approach is a bit faster but suffers from readability.
# Python3 program to convert a
# list into a tuple
def convert(list):
return (*list, )
# Driver function
list = [1, 2, 3, 4]
print(convert(list))
|
Python Exercise: Convert a list to a tuple
Last update on January 29 2021 07:13:37 (UTC/GMT +8 hours)Convert List to Tuple in Python
Because of the differences between a List and Tuple, you may need to convert a Python List to a Tuple in your application.
In this tutorial, we shall learn how to convert a list into tuple. There are many ways to do that. And we shall discuss the following methods with examples for each.
- Use tuple() builtin function.
- Unpack List inside parenthesis.
Use tuple() builtin function
tuple is a builtin Python class that can take any iterable as argument for its constructor and return a tuple object.
In the following example, we take a list and convert it to tuple using tuple() constructor.
Python Program
list1 = ['Saranya', 'Surya', 'Manu'] #convert list into tuple tuple1 = tuple(list1) print(tuple1) print(type(tuple1))RunOutput
('Saranya', 'Surya', 'Manu') <class 'tuple'>You may observe in the output that we have printed the type of the tuple1 object, which is <class 'tuple'>.
Unpack List inside Parenthesis
We can also unpack the items of List inside parenthesis, to create a tuple.
In the following program, we have a list, and we shall unpack the elements of this list inside parenthesis.
Python Program
list1 = ['Saranya', 'Surya', 'Ritha', 'Joy'] #unpack list items and form tuple tuple1 = (*list1,) print(tuple1) print(type(tuple1))RunOutput
Summary
In this tutorial of Python Examples, we learned how to convert a Python List into Tuple in different ways, with the help of well detailed example programs.
Related Tutorials
- Python Tuple vs List
- Python List of Tuples
- Python Sort List of Tuples
How to convert a list into a tuple in Python?
You can convert a list into a tuple simply by passing to the tuple function.
Python List list() Method
Method 1: List Comprehension + tuple()
Problem: How to convert a list of lists into a list of tuples?
Example: You’ve got a list of lists [[1, 2], [3, 4], [5, 6]] and you want to convert it into a list of tuples [(1, 2), (3, 4), (5, 6)].
Solution: There are different solutions to convert a list of lists to a list of tuples. Use list comprehension in its most basic form:
lst = [[1, 2], [3, 4], [5, 6]] tuples = [tuple(x) for x in lst] print(tuples) # [(1, 2), (3, 4), (5, 6)]Try It Yourself:
This approach is simple and effective. List comprehension defines how to convert each value (x in the example) to a new list element. As each list element is a new tuple, you use the constructor tuple(x) to create a new tuple from the list x.
If you have three list elements per sublist, you can use the same approach with the conversion:
lst = [[1, 2, 1], [3, 4, 3], [5, 6, 5]] tuples = [tuple(x) for x in lst] print(tuples) # [(1, 2, 1), (3, 4, 3), (5, 6, 5)]You can see the execution flow in the following interactive visualization (just click the “Next” button to see what’s happening in the code):
And if you have a varying number of list elements per sublist, this approach still works beautifully:
lst = [[1], [2, 3, 4], [5, 6, 7, 8]] tuples = [tuple(x) for x in lst] print(tuples) # [(1,), (2, 3, 4), (5, 6, 7, 8)]You see that an approach with list comprehension is the best way to convert a list of lists to a list of tuples. But are there any alternatives?