How to Iterate through a List of Tuples in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Table of Contents

  1. Iterate through a list of tuples in Python
  2. Iterate through a list of tuples with access to the index
  3. Iterate through a list of tuples by unpacking the items
  4. Iterate through a list of tuples using conversion to a dictionary

# 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')] # a # one # b # two # c # three for tup in my_list: for item in tup: print(item)

iterate through list of tuples

The code for this article is available on GitHub

The 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)

# Iterate through a list of tuples with access to the index

Alternatively, you can use the enumerate() function.

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])

iterate through list of tuples with access to index

The code for this article is available on GitHub

The enumerate() function takes an iterable and returns an enumerate object containing tuples where the first element is the index, and the second is 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

# Iterate through a list of tuples by unpacking the items

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

iterate through list of tuples by unpacking the items

The code for this article is available on GitHub

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 fewer values than there are in the tuple, you would get an error.

main.py
my_list = [('a', 'one'), ('b', 'two'), ('c', 'three')] # โ›”๏ธ ValueError 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.

You can also gather multiple tuple elements into a single variable.

main.py
my_list = [('a', 'one', 1), ('b', 'two', 2), ('c', 'three', 3)] for first, *rest in my_list: # a ['one', 1] # b ['two', 2] # c ['three', 3] print(first, rest)

On each iteration, we unpack the first tuple element and store the rest into the rest variable.

# Iterate through a list of tuples using conversion to a dictionary

You can also iterate through a list of tuples by converting the list to a dictionary.

main.py
my_list = [('a', 'one'), ('b', 'two'), ('c', 'three')] my_dict = dict(my_list) # ๐Ÿ‘‡๏ธ {'a': 'one', 'b': 'two', 'c': 'three'} print(my_dict) for key, value in my_dict.items(): # a one # b two # c three print(key, value)

iterate through list of tuples using conversion to dictionary

The code for this article is available on GitHub

We used the dict() class to convert the list of tuples to a dictionary.

The dict.items() method returns a new view of the dictionary's items ((key, value) pairs).

main.py
my_dict = {'id': 1, 'name': 'BobbyHadz'} # ๐Ÿ‘‡๏ธ dict_items([('id', 1), ('name', 'BobbyHadz')]) print(my_dict.items())

We used unpacking to assign the key and value to variables on each iteration.

You can also use the dict.keys() or dict.values() methods when iterating.

main.py
my_list = [('a', 'one'), ('b', 'two'), ('c', 'three')] my_dict = dict(my_list) # ๐Ÿ‘‡๏ธ {'a': 'one', 'b': 'two', 'c': 'three'} print(my_dict) for key in my_dict.keys(): # a # b # c print(key) # --------------------------------------- for value in my_dict.values(): # one # two # three print(value)

The dict.keys method returns a new view of the dictionary's keys.

main.py
my_dict = {'id': 1, 'name': 'BobbyHadz'} print(my_dict.keys()) # ๐Ÿ‘‰๏ธ dict_keys(['id', 'name'])

The dict.values method returns a new view of the dictionary's values.

main.py
my_dict = {'id': 1, 'name': 'bobbyhadz'} print(my_dict.values()) # ๐Ÿ‘‰๏ธ dict_values([1, 'bobbyhadz'])
The code for this article is available on GitHub
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.

Copyright ยฉ 2024 Borislav Hadzhiev