How to create a list of tuples in Python? We can create a list of tuples very easily because the list() can accept elements of any valid datatype including lists. A list of tuples is a very useful data structure in Python. By creating a list of tuples, you can combine multiple pieces of data into a single structure that is easy to work with. Tuples are immutable So, we can’t add, remove, or modify the elements of a tuple once they are created.
You can create a list of tuples in Python using many ways, for example, by using the list(), tuple(), zip(), list comprehension, and map() functions. In this article, I will explain how to create a list of tuples by using all these functions with examples.
1. Quick Examples of List of Tuples
If you are in a hurry, below are some quick examples of how to create a list of tuples.
# Quick examples of list of tuples
# Example 1: Initialize list of tuples using []
list_tuples = [(‘Python’, 2500), (‘Spark’, 2000), (‘Hadoop’, 3000)]
print(“List of tuples:”, list_tuples)
# Example 2: Create list of tuples using zip() function
list1 = [2500,2000,3000]
list2 = [‘Python’,’Spark’,’Hadoop’]
list_tuple = list(zip(list1,list2))
# Example 3: Create list of tuples using map() function
list1 = [[2500],[“Python”],[“Spark”],[3000]]
list_tuple = list(map(tuple, list1))
# Example 4: Create list of tuples
# Using list comprehension
list1 = [[‘Python’],[‘Spark’],[‘Hadoop’]]
list_tuple = [tuple(x) for x in list1]
2. Create List of Tuples in Python
In Python, a list of tuples is a collection of ordered and indexed data, where each item in the list is a tuple. You can create a list of tuples using square brackets to define the list and enclose each tuple in parentheses. For example, Let’s create a list of tuples with three items where each item in the list is a tuple that contains an integer and a string.
# Create list of tuples using list()
list_tuples = [(‘Python’, 2500), (‘Spark’, 2000), (‘Hadoop’, 3000)]
print(“List of tuples:”, list_tuples)
Yields below output.
3. Create List of Tuples Using zip() Function
Thee zip() function also be used to create list of tuples. For example, zip() takes in two lists (list1, list2) and creates a list of tuples where the tuple contains the element from each of the input lists. The resulting list of tuples is then stored list_tuples and printed to the console.
# Initialize the lists
list1 = [2500,2000,3000]
list2 = [‘Python’,’Spark’,’Hadoop’]
# Create list of tuples using zip() function
list_tuple = list(zip(list1,list2))
print(“List of tuples:”, list_tuples)
Yields below output.
4. Create List of Tuples Using map() Function
You can create a list of tuples from a list of lists using the map() function and the tuple() constructor. The map() function takes two arguments, a function and an iterable. In this case, the tuple() function is used to convert each inner list into a tuple. The resulting tuples are then combined into a new list using the list() function.
# List of tuples using map() function
list1 = [[2500],[“Python”],[“Spark”],[3000]]
list_tuple = list(map(tuple, list1))
print(“List of tuples:”, list_tuples)
# Output:
# List of tuples: [(2500,), (‘Python’,), (‘Spark’,), (3000,)]
5. Using List Comprehension Create List of Tuples
You can also use the list comprehension and tuple() method. For instance, list_tuple = [tuple(x) for x in list1] creates a new list of tuples by iterating over the elements of list1 and converting each element to a tuple using the tuple() function.
# Using list comprehension
# And tuple() method
list1 = [[‘Python’],[‘Spark’],[‘Hadoop’]]
list_tuple = [tuple(x) for x in list1]
print(“List of tuples:”, list_tuples)
# Output:
# List of tuples: [(‘Python’,), (‘Spark’,), (‘Hadoop’,)]
6. Operation on List of Tuples
So far you have learned different ways to create a list of tuples in Python. In this section, let’s perform a few operations on the list of tuples.
6.1 Access Element from List of Tuples
We know that, list is mutable and ordered so, we can modify the list after creating it. You can access items from the list of tuples using indexing. For example, to access the second item in the list.
# Access elements from List of tuples
# using indexing
# Initialize the List of tuples
list_tuples = [(‘Python’, 2500), (‘Spark’, 2000), (‘Hadoop’, 3000)]
result = list_tuples[1]
print(“List of tuples:n”, list_tuples)
print(“Access element from list:n”, result)
Yields below output.
You can also access individual elements within each tuple using nested indexing. For example, to access the string 3000 in the third tuple.
# Access individual elements
result = list_tuples[2][1]
print(“Access element from tuple:”, result)
# Output:
# Access element from tuple: 3000
6.2 Append Tuple to List of Tuples
You can append a tuple to a list of tuples using the append() method. For example, you defined a list of tuples list_tuples and used the append() method to add a new tuple (‘Pandas’, 2200) to the end of the list. The append() method modifies the original list in place, so you don’t need to assign the result to a new variable.
# Initialize the List of tuple
list_tuples = [(‘Python’, 2500), (‘Spark’, 2000), (‘Hadoop’, 3000)]
print(“Orginal list of tuples: “,list_tuples)
# Append a tuple to the list of tuples
list_tuples.append((‘Pandas’, 2200))
print(“Afetr appended list of tuples: “, list_tuples)
Yields below output.
# Output:
# Orginal list of tuples: [(‘Python’, 2500), (‘Spark’, 2000), (‘Hadoop’, 3000)]
# After appended list of tuples: [(‘Python’, 2500), (‘Spark’, 2000), (‘Hadoop’, 3000), (‘Pandas’, 2200)]
6.3. Remove a Tuple from the List of Tuples
You can also remove an element from a list in Python using the del keyword. For instance, first, initialize a list of tuples named list_tuples, then remove a tuple from the list using the del statement. It removes the third tuple from the list using the del statement. In this case, the third tuple is (‘Hadoop’, 3000).
# List of tuple initialization
list_tuples = [(‘Python’, 2500), (‘Spark’, 2000), (‘Hadoop’, 3000)]
print(“Orginal list of tuples: “,list_tuples)
# Remove a tuple from the list of tuples
del list_tuples[2]
print(“Updated a list of tuples: “, list_tuples)
# Output:
# Orginal list of tuples: [(‘Python’, 2500), (‘Spark’, 2000), (‘Hadoop’, 3000)]
# Updated a list of tuples: [(‘Python’, 2500), (‘Spark’, 2000)]
6.4 Update a Tuple in the List of Tuples
You can also update a tuple in the list of tuples in Python. For example, first, initialize a list of tuples called list_tuples, then update a tuple in the list by replacing it with a new tuple. It updates the second tuple in the list by replacing it with a new tuple containing the values (‘Pandas’,22000). So, the tuple (‘Hadoop’, 3000) was removed from the list, and the resulting list contains only two tuples, (‘Python’, 2500) and (‘Spark’, 2000).
# List of tuple initialization
list_tuples = [(‘Python’, 2500), (‘Spark’, 2000), (‘Hadoop’, 3000)]
print(“Orginal list of tuples: “,list_tuples)
# Update a tuple in list of tuples
list_tuples[1] = (‘Pandas’,22000)
print(“Updated a list of tuples: “, list_tuples)
# Output:
# Orginal list of tuples: [(‘Python’, 2500), (‘Spark’, 2000), (‘Hadoop’, 3000)]
# Updated a list of tuples: [(‘Python’, 2500), (‘Pandas’, 22000), (‘Hadoop’, 3000)]
7. Conclusion
In this article, I have explained how to create a list of tuples in Python by using the List(), tuple(), zip(), map(), and list comprehension with examples. And also explained how to update the list of tuples using Python functions and operators.
Happy Learning !!
How to create a list of tuples in Python? We can create a list of tuples very easily because the list() can accept elements of any valid datatype including lists. A list of tuples is a very useful data structure in Python. By creating a list of tuples, you can combine multiple pieces of data Read More Python, Python Tutorial, Python List Examples, python tuple examples