Last updated: Apr 9, 2024
Reading timeยท3 min
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.
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)
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.
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.
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 is the item.
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.
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.
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.
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.
ValueError
.If you don't need to store a certain value, use an underscore for the variable's name.
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.
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.
You can also iterate through a list of tuples by converting the list to a dictionary.
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)
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).
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.
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.
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.
my_dict = {'id': 1, 'name': 'bobbyhadz'} print(my_dict.values()) # ๐๏ธ dict_values([1, 'bobbyhadz'])