Python – Create a List of Tuples
Last Updated :
27 Dec, 2023
List and tuples both are data structures in Python, with some differences. Lists are mutable data structures whereas tuples are immutable (can not be changed after creating it).
We can make a list that contains tuples as elements. This practice is useful for data structuring, memory efficiency, and data security as tuples are immutable. In this article, we will discuss multiple ways by which we can create a list of tuples in Python.
How to Create a List of Tuples in Python
To create and return list of tuples in Python, you can use the following methods:
- list() and tuple() method.
- zip() method
- zip() and iter() method
- map() method
- List comprehension and tuple() method
- Using built-in functions
Create a List of Tuples Using list() and tuple() methods
We can create a list of tuples using list and tuples directly.
Syntax: [(tuple1),(tuple2),(tuple3),..,(tuple n)]
Example: Python code to create a list of tuples using list and tuple
Python3
data = [( 1 , 'sravan' ), ( 2 , 'ojaswi' ), ( 3 , 'bobby' ),
( 4 , 'rohith' ), ( 5 , 'gnanesh' )]
data
|
Output:
[(1, 'sravan'), (2, 'ojaswi'), (3, 'bobby'), (4, 'rohith'), (5, 'gnanesh')]
Create a List of Tuples Using zip() function
Using the zip() function we can create a list of tuples from n lists.
Syntax: list(zip(list1,list2,.,listn)
Here, lists are the data (separate lists which are elements like tuples in the list
Example: Python program to create two lists with college ID and name and create a list of tuples using zip() function
Python3
roll_no = [ 1 , 2 , 3 , 4 , 5 ]
name = [ 'sravan' , 'ojaswi' , 'bobby' , 'rohith' , 'gnanesh' ]
data = list ( zip (roll_no, name))
data
|
Output:
[(1, 'sravan'), (2, 'ojaswi'), (3, 'bobby'), (4, 'rohith'), (5, 'gnanesh')]
Create a List of Tuples Using Using zip() and iter() method
Here we are going to form a list of tuples using iter() function along with zip() function.
Syntax: [x for x in zip(*[iter(list)])]
where x is the iterator to iterate in the list, zip is used to zip the list, and iter() is used to iterate through the entire list
Example: Python code to create a list of tuples by forming a list of tuples
Python3
name = [ 'sravan' , 'ojaswi' , 'bobby' , 'rohith' , 'gnanesh' ]
data = [x for x in zip ( * [ iter (name)])]
data
|
Output:
[('sravan',), ('ojaswi',), ('bobby',), ('rohith',), ('gnanesh',)]
Create a List of Tuples Using map() function
Here we are passing the data in a list and then using the map() function we can create a list of tuples
Syntax: list(map(tuple, list_data))
Here, list_data is the input list to create a list of tuples, the list is a predefined function and the tuple is a predefined function
Example: Python code to create a list of tuples from the list using map() function
Python3
name = [[ 'sravan' ], [ 'ojaswi' ], [ 'bobby' ],
[ 'rohith' ], [ 'gnanesh' ]]
data = list ( map ( tuple , name))
data
|
Output:
[('sravan',), ('ojaswi',), ('bobby',), ('rohith',), ('gnanesh',)]
Create a List of Tuples Using list comprehension and tuple() method
Here we are using List comprehension and tuple() to create a list of tuples.
Syntax:
[tuple(x) for x in list_data]
where tuple(x) is an iterator to convert iterative objects to a tuple and list_data is the input data
Example: Python code to create a list of tuples using list comprehension and tuple() method
Python3
name = [[ 'sravan' ], [ 'ojaswi' ], [ 'bobby' ],
[ 'rohith' ], [ 'gnanesh' ]]
data = [ tuple (x) for x in name]
data
|
Output:
[('sravan',), ('ojaswi',), ('bobby',), ('rohith',), ('gnanesh',)]
Create a List of Tuples without using built-in functions
Here’s an example of how you can create a list of tuples without using any built-in functions like list() or tuple().
Python3
def create_list_of_tuples(lst1, lst2):
result = []
for i in range ( len (lst1)):
tuple_element = (lst1[i], lst2[i])
result.append(tuple_element)
return result
list1 = [ 1 , 2 , 3 ]
list2 = [ 'a' , 'b' , 'c' ]
list_of_tuples = create_list_of_tuples(list1, list2)
print (list_of_tuples)
|
Output:
[(1, 'a'), (2, 'b'), (3, 'c')]
Using the above methods and printing the list will return a list of tuples. Creating or returning a list of tuples is easier than you thought, we have explained 6 ways to create a list of tuples.
Also Read:
Please Login to comment...