Iterate through a list of tuples in Python

avatar

Borislav Hadzhiev

Last updated: Jun 30, 2022

banner

Photo from Unsplash

Iterate through a list of tuples in Python #

Use a nested for loop to iterate through a list of tuples. The first for loop is used to iterate over the list, and the second for loop is used to iterate over each tuple in the list.

main.py
my_list = [('a', 'one'), ('b', 'two'), ('c', 'three')] # 👇️ using nested for loop for tup in my_list: for item in tup: print(item) print('---------------------------') # 👇️ iterate over list of tuples with index for index, tup in enumerate(my_list): print(index) print(tup[0]) print(tup[1]) print('---------------------------') # 👇️ iterate over list of tuples with unpacking for first, second in my_list: print(first, second)

The first example uses a nested for loop to iterate through a list of tuples.

The outer for loop iterates over the list, and the inner for loop is used to iterate over each tuple in the list.

main.py
my_list = [('a', 'one'), ('b', 'two'), ('c', 'three')] # 👇️ using nested for loop for tup in my_list: for item in tup: print(item)

Alternatively, you can use the enumerate() function.

Use the enumerate() function to iterate over a list of tuples, e.g. for index, tup in enumerate(my_list):. The enumerate function returns an object that contains tuples where the first item is the index, and the second is the value.

main.py
my_list = [('a', 'one'), ('b', 'two'), ('c', 'three')] for index, tup in enumerate(my_list): print(index) print(tup[0]) print(tup[1])

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index, and the second - the item.

main.py
my_list = ['apple', 'banana', 'melon'] for index, item in enumerate(my_list): print(index, item) # 👉️ 0 apple, 1 banana, 2 melon

Alternatively, you can unpack the values from each tuple to iterate over a list of tuples.

main.py
my_list = [('a', 'one'), ('b', 'two'), ('c', 'three')] for first, second in my_list: print(first, second) # 👇️ # a one # b two # c three

When unpacking, make sure to declare exactly as many variables as there are items in the tuple.

main.py
first, second = ('a', 'one') print(first) # 👉️ a print(second) # 👉️ one

When unpacking from a tuple, each variable declaration counts for a single item.

If you try to unpack more or less values than there are in the tuple, you would get an error.

main.py
my_list = [('a', 'one'), ('b', 'two'), ('c', 'three')] # ⛔️ ValueError: not enough values to unpack (expected 3, got 2) for first, second, third in my_list: print(first, second, third)

We declared 3 variables, but each tuple contains only 2 items.

The inconsistency between the number of variables and the number of items in the tuple causes a ValueError.

If you don't need to store a certain value, use an underscore for the variable's name.

main.py
my_list = [('a', 'one'), ('b', 'two'), ('c', 'three')] for first, _ in my_list: print(first) # 👇️ # a # b # c

When you use an underscore for a variable's name, you indicate to other developers that this variable is just a placeholder.

You can use as many underscores as necessary when unpacking values.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.