Open In App

Python – Create a List of Tuples

Last Updated : 27 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. list() and tuple() method.
  2. zip() method
  3. zip() and iter() method
  4. map() method
  5. List comprehension and tuple() method
  6. 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




# create tuples with college id and
# name and store in a list
data = [(1, 'sravan'), (2, 'ojaswi'), (3, 'bobby'),
        (4, 'rohith'), (5, 'gnanesh')]
 
# display data
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




# create two lists  with college id and name
roll_no = [1, 2, 3, 4, 5]
name = ['sravan', 'ojaswi', 'bobby', 'rohith', 'gnanesh']
 
# zip the two lists using zip() function
data = list(zip(roll_no, name))
 
# display data
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




# create a list with name
name = ['sravan', 'ojaswi', 'bobby', 'rohith', 'gnanesh']
 
# zip the two lists using iter() function
data = [x for x in zip(*[iter(name)])]
 
# display data
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




# create a list with name
name = [['sravan'], ['ojaswi'], ['bobby'],
        ['rohith'], ['gnanesh']]
 
# create list of tuple using above
# list using map function
data = list(map(tuple, name))
 
# display data
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




# create a list with name
name = [['sravan'], ['ojaswi'], ['bobby'],
        ['rohith'], ['gnanesh']]
 
# create list of tuple using above list
# using  list comprehension and tuple()
# method
data = [tuple(x) for x in name]
 
# display data
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




# Function to create a list of tuples
def create_list_of_tuples(lst1, lst2):
    result = []  # Empty list to store the tuples
    for i in range(len(lst1)):
        # Create a tuple from corresponding elements
        tuple_element = (lst1[i], lst2[i])
        result.append(tuple_element)  # Append the tuple to the list
    return result
 
 
# Example usage
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:



Previous Article
Next Article

Similar Reads

Python | Remove duplicate tuples from list of tuples
Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x')] Method #1 : List comprehension This is a naive
5 min read
Python | Find the tuples containing the given element from a list of tuples
Given a list of tuples, the task is to find all those tuples containing the given element, say n. Examples: Input: n = 11, list = [(11, 22), (33, 55), (55, 77), (11, 44)] Output: [(11, 22), (11, 44)] Input: n = 3, list = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)] Output: [(14, 3), (1, 3), (3, 3)] There are multiple ways we can find the tuples contai
6 min read
Python | Remove tuples from list of tuples if greater than n
Given a list of a tuple, the task is to remove all the tuples from list, if it's greater than n (say 100). Let's discuss a few methods for the same. Method #1: Using lambda STEPS: Initialize a list of tuples: ini_tuple = [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)]Print the initial list: print("intial_list", str(ini_tuple))Define the
6 min read
Python | Remove tuples having duplicate first value from given list of tuples
Given a list of tuples, the task is to remove all tuples having duplicate first values from the given list of tuples. Examples: Input: [(12.121, 'Tuple1'), (12.121, 'Tuple2'), (12.121, 'Tuple3'), (923232.2323, 'Tuple4')] Output: [(12.121, 'Tuple1'), (923232.2323, 'Tuple4')]Input: [('Tuple1', 121), ('Tuple2', 125), ('Tuple1', 135), ('Tuple4', 478)]
7 min read
Python | Count tuples occurrence in list of tuples
Many a time while developing web and desktop products in Python, we use nested lists and have several queries about how to find the count of unique tuples. Let us see how to get the count of unique tuples in the given list of tuples. Below are some ways to achieve the above task. Method #1: Using Iteration C/C++ Code # Python code to count unique #
5 min read
Python | Combining tuples in list of tuples
Sometimes, we might have to perform certain problems related to tuples in which we need to segregate the tuple elements to combine with each element of complex tuple element( such as list ). This can have application in situations we need to combine values to form a whole. Let's discuss certain ways in which this can be performed. Method #1: Using
7 min read
Python | Convert string tuples to list tuples
Sometimes, while working with Python we can have a problem in which we have a list of records in form of tuples in stringified form and we desire to convert them to a list of tuples. This kind of problem can have its occurrence in the data science domain. Let's discuss certain ways in which this task can be performed. Method 1 (Using eval() + list
4 min read
Python - Filter all uppercase characters Tuples from given list of tuples
Given a Tuple list, filter tuples that contain all uppercase characters. Input : test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"), ("GfG", ), ("Gfg", "CS")] Output : [('GFG', 'IS', 'BEST')] Explanation : Only 1 tuple has all uppercase Strings. Input : test_list = [("GFG", "iS", "BEST"), ("GFg", "AVERAGE"), ("GfG", ), ("Gfg", "CS")] Output : [
8 min read
Python program to find Tuples with positive elements in List of tuples
Given a list of tuples. The task is to get all the tuples that have all positive elements. Examples: Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)] Output : [(4, 5, 9)] Explanation : Extracted tuples with all positive elements. Input : test_list = [(-4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)] Output : [] Explanation : No tuple wit
10 min read
Python program to find tuples which have all elements divisible by K from a list of tuples
Given a list of tuples. The task is to extract all tuples which have all elements divisible by K. Input : test_list = [(6, 24, 12), (60, 12, 6), (12, 18, 21)], K = 6 Output : [(6, 24, 12), (60, 12, 6)] Explanation : Both tuples have all elements multiple of 6. Input : test_list = [(6, 24, 12), (60, 10, 5), (12, 18, 21)], K = 5 Output : [(60, 10, 5)
7 min read
Practice Tags :
three90RightbarBannerImg