Iterate through list of dictionaries in Python
Last Updated :
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'}]
Method 1: Using indexing
This is a direct method, where list elements are extracted using just the index.
Syntax:
list[index]
Example:
Python3
# Create a list of dictionaries
languages = [
{
"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 dictionaries
languages = [
{
"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
Method 2: Using keys()
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 dictionaries
languages = [
{
"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 list
for 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 dictionaries
languages = [
{
"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
Similar Reads
Flatten given list of dictionaries - Python We are given a list of dictionaries, and the task is to combine all the key-value pairs into a single dictionary. For example, if we have: d = [{'a': 1}, {'b': 2}, {'c': 3}] then the output will be {'a': 1, 'b': 2, 'c': 3}Using the update() methodIn this method we process each dictionary in the list
3 min read
Iterate over a dictionary in Python In this article, we will cover How to Iterate Through a Dictionary in Python. To Loop through values in a dictionary you can use built-in methods like values(), items() or even directly iterate over the dictionary to access values with keys.How to Loop Through a Dictionary in PythonThere are multipl
6 min read
Convert a list of Tuples into Dictionary - Python Converting a list of tuples into a dictionary involves transforming each tuple, where the first element serves as the key and the second as the corresponding value. For example, given a list of tuples a = [("a", 1), ("b", 2), ("c", 3)], we need to convert it into a dictionary. Since each key-value p
3 min read
Initializing dictionary with Empty Lists in Python In Python, it's common to encounter scenarios where you need to store lists in a dictionary. Often, this involves checking if a key exists and then creating a list for that key. However, a more efficient approach is to initialize the dictionary keys with empty lists from the start. Let's explore som
5 min read
How to use a List as a key of a Dictionary in Python 3? In Python, we use dictionaries to check if an item is present or not . Dictionaries use key:value pair to search if a key is present or not and if the key is present what is its value . We can use integer, string, tuples as dictionary keys but cannot use list as a key of it . The reason is explained
3 min read
Dictionaries in Python Python dictionary is a data structure that stores the value in key: value pairs. Values in a dictionary can be of any data type and can be duplicated, whereas keys can't be repeated and must be immutable. Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier to
5 min read