Parse List of Dictionaries in Python (3 Examples)
In this tutorial, you’ll learn how to parse a list of dictionaries in Python. Parsing involves extracting specific information or values from each dictionary within the list. We’ll explore different techniques to parse the data effectively.
The table of contents is structured as follows:
Let’s get started!
Initializing Sample List of Dictionaries
Firstly, let’s initialize a sample list of dictionaries that we’ll be working with throughout the tutorial:
# Initializing a list of dictionaries list_of_dicts = [ {'name': 'John', 'age': 25, 'country': 'USA'}, {'name': 'Alice', 'age': 30, 'country': 'Canada'}, {'name': 'Bob', 'age': 28, 'country': 'USA'}, {'name': 'Mike', 'age': 35, 'country': 'Australia'} ]
This creates a list of dictionaries, where each dictionary represents an individual item with various key-value pairs. Each dictionary contains information about a person, including their name, age, and country.
Example 1: Parsing Single Key Values
The simplest form of parsing involves extracting specific key values from each dictionary in the list. Here’s an example:
# Parse a single key-value pair ages = [d['age'] for d in list_of_dicts] # Print the parsed values print(ages) # [25, 30, 28, 35]
In this example, we use a list comprehension to iterate over each dictionary in the list and extract the value corresponding to the age
key.
The extracted values are stored in the ages
list. Finally, we print the parsed values. You can modify the code to parse other specific key values by replacing 'age'
with the desired key.
Example 2: Parsing Multiple Key-Value Pairs
Sometimes, you may need to parse multiple key-value pairs from each dictionary. Here’s an example of how to achieve that:
# Parse multiple key-value pairs details = [{'name': d['name'], 'country': d['country']} for d in list_of_dicts] # Print the parsed values for item in details: print("Name:", item['name']) print("Country:", item['country']) print() # Name: John # Country: USA # Name: Alice # Country: Canada # Name: Bob # Country: USA # Name: Mike # Country: Australia
In this example, we use a list comprehension to iterate over each dictionary in the list and extract the values corresponding to the name
and country
keys.
We create a new dictionary for each item containing only these key-value pairs. The resulting list of dictionaries is stored in the details
list. Finally, we iterate over the details
list and print the parsed values.
You can customize the code to parse any desired combination of key-value pairs by modifying the keys in the list comprehension.
Example 3: Parsing Conditionally
In some cases, you may want to parse specific data based on certain conditions. Here’s an example of conditional parsing:
# Conditional parsing us_citizens = [d['name'] for d in list_of_dicts if d['country'] == 'USA'] # Print the parsed values print(us_citizens) # ['John', 'Bob']
In this example, we use a list comprehension to iterate over each dictionary in the list and extract the names of individuals who have the country value as 'USA'
.
We apply a condition if d['country'] == 'USA'
to filter the dictionaries based on the country value. The extracted names are stored in the us_citizens
list. Finally, we print the parsed values.
You can modify the condition and the parsed key to suit your specific requirements.
Video, Further Resources & Summary
In this tutorial, we explored different techniques for parsing a list of dictionaries in Python. We learned how to extract specific key values, parse multiple key-value pairs, and perform conditional parsing. These techniques allow you to efficiently extract and manipulate data from a list of dictionaries.
Do you need more explanations on looping through a list of integers in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
For more information and examples related to Python programming, you can check out the following tutorials on Statistics Globe:
- Using Lists in Python (Introduction)
- Convert List to JSON Object & Vice Versa in Python (Examples)
- Extract First & Last N Columns from pandas DataFrame in Python (2 Examples)
- Convert Nested Dictionary to List in Python (2 Examples)
- Convert List to Dictionary with Index as Values in Python (2 Examples)
Now you have the knowledge and tools to parse a list of dictionaries in Python. Happy coding!
This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.
Statistics Globe Newsletter