Contents
List of Tuples in Python
List is a collection of items. And tuple can be considered as an item. So, you can have a List of Tuples in Python.
In this tutorial, we will learn how to initialize a list of tuples and some of the operations on this list of tuples.
Initialize List of Tuple
Following is an example to initialize a list of tuples.
list_of_tuples = [(1,'Saranya',92), (2,'Surya',95), (3,'Manu',93)]
Run In the above list, we have three tuples in the list.
Append Tuple to List of Tuples
Use append() method to append an item, in this case a tuple, to the list.
Python Program
list_of_tuples = [(1,'Saranya',92), (2,'Surya',95), (3,'Manu',93)]
list_of_tuples.append((4, 'Reshmi', 94))
print(list_of_tuples)
Run Output
[(1, 'Saranya', 92), (2, 'Surya', 95), (3, 'Manu', 93), (4, 'Reshmi', 94)]
Update a Tuple in List of Tuples
You can replace a tuple with other tuple, but not modify a tuple in-place in the list, because tuples are immutable.
Python Program
list_of_tuples = [(1,'Saranya',92), (2,'Surya',95), (3,'Manu',93)]
list_of_tuples[1] = (2, 'Reshmi', 94)
print(list_of_tuples)
Run Output
[(1, 'Saranya', 92), (2, 'Reshmi', 94), (3, 'Manu', 93)]
Remove a Tuple from List of Tuples
Use del keyword to remove any tuple, using index, from a list of tuples, as shown in this example program.
Python Program
list_of_tuples = [(1,'Saranya',92), (2,'Surya',95), (3,'Manu',93)]
del list_of_tuples[2]
print(list_of_tuples)
Run Output
[(1, 'Saranya', 92), (2, 'Surya', 95)]
Summary
In this tutorial of Python Examples, we learned how to initialize, access and modify a list of tuples, with the help of well detailed Python example programs.