Iterate through list of dictionaries in Python - GeeksforGeeks

22 Nov, 2021
In this article, we will learn how to iterate through a list of dictionaries. 
List of dictionaries in use:
[{‘Python’: ‘Machine Learning’, ‘R’: ‘Machine learning’}, 
{‘Python’: ‘Web development’, ‘Java Script’: ‘Web Development’, ‘HTML’: ‘Web Development’}, 
{‘C++’: ‘Game Development’, ‘Python’: ‘Game Development’}, {‘Java’: ‘App Development’, ‘Kotlin’: ‘App Development’}]
This is a direct method, where list elements are extracted using just the index.
Syntax:
list[index]
Example:
Python3
# Create a list of dictionarieslanguages = [    {        "Python": "Machine Learning",        "R": "Machine learning",    },    {        "Python": "Web development",        "Java Script": "Web Development",        "HTML": "Web Development"    },    {        "C++": "Game Development",        "Python": "Game Development"    },    {        "Java": "App Development",        "Kotlin": "App Development"    }]  print(languages[0])print(languages[1])print(languages[2])print(languages[3])
Output:
{‘Python’: ‘Machine Learning’, ‘R’: ‘Machine learning’}
{‘Python’: ‘Web development’, ‘Java Script’: ‘Web Development’, ‘HTML’: ‘Web Development’}
{‘C++’: ‘Game Development’, ‘Python’: ‘Game Development’}
{‘Java’: ‘App Development’, ‘Kotlin’: ‘App Development’}
After using indexing to particular dictionaries, now we can treat each item of the list as a dictionary,
Example: Extracting values from a particular dictionary
Python3
# Create a list of dictionarieslanguages = [    {        "Python": "Machine Learning",        "R": "Machine learning",    },    {        "Python": "Web development",        "Java Script": "Web Development",        "HTML": "Web Development"    },    {        "C++": "Game Development",        "Python": "Game Development"    },    {        "Java": "App Development",        "Kotlin": "App Development"    }] for key, val in languages[0].items():    print("{} : {}".format(key, val))
Output:
Python : Machine Learning
R : Machine learning
After iterating to a list the keys from the dictionary can further be extracted using the keys() function.
Example: Extracting key values
Python3
# Create a list of dictionarieslanguages = [    {        "Python": "Machine Learning",        "R": "Machine learning",    },    {        "Python": "Web development",        "Java Script": "Web Development",        "HTML": "Web Development"    },    {        "C++": "Game Development",        "Python": "Game Development"    },    {        "Java": "App Development",        "Kotlin": "App Development"    }] # iterate over the listfor i in languages:       # now i is a dict, now we see the keys    # of the dict    for key in i.keys():               # print every key of each dict        print(key)     print("-------------")
Output:
Python
R
————-
Python
Java Script
HTML
————-
C++
Python
————-
Java
Kotlin
————-
The list is simply iterated using list comprehension and the dictionaries are printed.
Example: Extracting keys using list comprehension
Python3
# Create a list of dictionarieslanguages = [    {        "Python" : "Machine Learning",        "R" : "Machine learning",    },    {        "Python" : "Web development",        "Java Script" : "Web Development",        "HTML" : "Web Development"    },    {        "C++" : "Game Development",        "Python" : "Game Development"    },    {        "Java" : "App Development",        "Kotlin" : "App Development"    }] # here we are printing the keys of the dictionary# by using list comprehension and each key will be# printed in a new line due to the presence of " sep = "\n" ".# It will add a new line character to our output. print(*[key for i in languages for key in i.keys()], sep = "\n")
Output:
Python
R
Python
Java Script
HTML
C++
Python
Java
Kotlin
as5853535
Picked
Python List-of-Dict
python-dict
Python
python-dict
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Python | Get unique values from a list
Create a directory in Python
Defaultdict in Python
Python | Pandas dataframe.groupby()