In this post, we are going to understand how to Loop through a list of dictionaries in Python with examples. The list contains multiple dictionaries is a list of dictionaries. First will iterate through the list of dictionaries using for loop and then use the inner loop to iterate through each dictionaries key and values.
1.Loop through list of dictionaries python
In these examples, We have an outer for loop to iterate over the list and an inner loop to iterate over each dictionary key and value by using the items() method. This is how we iterate over the list of dictionaries using for loop.
list_of_dicts = [{'Name': 'Jack','Mark': 100, 'Subject': 'Math'}, { 'Name': 'Rack', 'Mark': 100,'Subject:':'Math'},
{ 'Name': 'Max','Mark': 100, 'Subject': 'Music'}]
for dic in list_of_dicts:
for key,val in dic.items():
print(f'{key}: {val}')
Output
Name: Jack
Mark: 100
Subject: Math
Name: Rack
Mark: 100
Subject:: Math
Name: Max
Mark: 100
Subject: Music
2. iterate over List of dictionaries using custom function
Sometimes We have a list of dictionaries along with dictionary names and need to check if the nested list of items is our dictionary.
We have defined a custom function iterate_list_dicts to verify if the input is dictionary using isinstance() when it is the dictionary that iterates over the dictionary by using items() method else printing key-value pairs.
import json
data_dict =[{'Record1':{'Name': 'Jack','Mark': 100, 'Subject': 'Math'}},
{'Record2':{ 'Name': 'Rack', 'Mark': 100,'Subject:':'Math'}},
{'Record3':{ 'Name': 'Max','Mark': 100, 'Subject': 'Music'}}
]
def iterate_list_dicts(data_dict):
for dic in data_dict:
for key,value in dic.items():
if not isinstance(value, dict):
print(key,value)
else:
for key,value in value.items():
print (key,value )
iterate_list_dicts(data_dict)
Output
Name Jack
Mark 100
Subject Math
Name Rack
Mark 100
Subject: Math
Name Max
Mark 100
Subject Music
3. List comprehension to Loop through list of dictionaries
In this Python program example, we are iterate over the list of dictionaries by using the list comprehension
data_dict =[{'Record1':{'Name': 'Jack','Mark': 100, 'Subject': 'Math'}},
{'Record2':{ 'Name': 'Rack', 'Mark': 100,'Subject:':'Math'}},
{'Record3':{ 'Name': 'Max','Mark': 100, 'Subject': 'Music'}}
]
print(*[val for dic in data_dict for val in dic.values()], sep='\n')
Output
{'Name': 'Jack', 'Mark': 100, 'Subject': 'Math'}
{'Name': 'Rack', 'Mark': 100, 'Subject:': 'Math'}
{'Name': 'Max', 'Mark': 100, 'Subject': 'Music'}
4. Iterate over list of dictionaries single key
Sometimes we have the need to iterate over the dictionary single key-value pair instead of a whole list of dictionaries. In this Python program example, we are going to iterate over the key ‘name’ of the list of dictionaries.
data_dict =[{'Name': 'Jack','Mark': 100, 'Subject': 'Math'},
{'Name': 'Rack', 'Mark': 100,'Subject:':'Math'},
{'Name': 'Max','Mark': 100, 'Subject': 'Music'}]
for i, Name in enumerate(key['Name'] for key in data_dict):
print(i,Name)
Output
0 Jack
1 Rack
2 Max
5. iterate over list of dictionaries using len()
In this Python program example, we are iterating over the list of dictionaries by using the range() function that takes dictionary length as an argument.
We are finding the index of each and looping over the dictionary key-value pair.
data_dict =[{'Name': 'Jack','Mark': 100, 'Subject': 'Math'},
{'Name': 'Rack', 'Mark': 100,'Subject:':'Math'},
{'Name': 'Max','Mark': 100, 'Subject': 'Music'}]
for index in range(len(data_dict)):
for key in data_dict[index]:
print(data_dict[index][key])
Output
Jack
100
Math
Rack
100
Math
Max
100
Music
Summary
In this post, we have learned how to Loop through a list of dictionaries Python with examples.