Python – Create a List of Tuples

28 Jul, 2021
In this article, we will discuss multiple ways by which we can create a list of tuples in Python.
we can create a list of tuples using list and tuples directly.
Syntax:
[(tuple1),(tuple2),(tuple3),..,(tuple n)]
Example: Python code to create list of tuples using list and tuple
Python3
# create tuples with college id and# name and store in a listdata = [(1, 'sravan'), (2, 'ojaswi'), (3, 'bobby'),        (4, 'rohith'), (5, 'gnanesh')]  # display datadata
Output:
[(1, ‘sravan’), (2, ‘ojaswi’), (3, ‘bobby’), (4, ‘rohith’), (5, ‘gnanesh’)]
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
# create two lists  with college id and nameroll_no = [1, 2, 3, 4, 5]name = ['sravan', 'ojaswi', 'bobby', 'rohith', 'gnanesh']  # zip the two lists using zip() functiondata = list(zip(roll_no, name))  # display datadata
Output:
[(1, ‘sravan’), (2, ‘ojaswi’), (3, ‘bobby’), (4, ‘rohith’), (5, ‘gnanesh’)]
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
# create a list with namename = ['sravan', 'ojaswi', 'bobby', 'rohith', 'gnanesh']  # zip the two lists using iter() functiondata = [x for x in zip(*[iter(name)])]  # display datadata
Output:
[(‘sravan’,), (‘ojaswi’,), (‘bobby’,), (‘rohith’,), (‘gnanesh’,)]
Here we are passing the data in 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, list is a predefined function and tuple is a predefined function
Example: Python code to create a list of tuples from the list using map() function
Python3
# create a list with namename = [['sravan'], ['ojaswi'], ['bobby'],         ['rohith'], ['gnanesh']]  # create list of tuple using above # list using map functiondata = list(map(tuple, name))  # display datadata
Output:
[(‘sravan’,), (‘ojaswi’,), (‘bobby’,), (‘rohith’,), (‘gnanesh’,)]
Here we are using 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 tuple and list_data is the input data
Example: Python code to create a list of tuples using list comprehension and tuple() method
Python3
# create a list with namename = [['sravan'], ['ojaswi'], ['bobby'],        ['rohith'], ['gnanesh']]  # create list of tuple using above list# using  list comprehension and tuple() # methoddata = [tuple(x) for x in name]  # display datadata
Output:
[(‘sravan’,), (‘ojaswi’,), (‘bobby’,), (‘rohith’,), (‘gnanesh’,)]
Picked
Python List-of-Tuples
python-list
python-tuple
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Iterate over a list in Python
Python Classes and Objects
Convert integer to string in Python