Open In App

Filter List of Dictionaries Based on Key Values in Python

Last Updated : 30 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Filtering a list of dictionaries based on key values is a common task in Python, especially when working with data manipulation and analysis. In this article, we will explore five different methods to achieve this goal, each offering its own advantages and use cases. Whether you’re a beginner or an experienced Python developer, these methods will provide you with diverse approaches to efficiently filter your data.

Filter List Of Dictionaries Based On Key Values

Below, are the ways to Filter a List Of Dictionaries Based On Key Values in Python.

Filter List Of Dictionaries using comparison

In this example, the below code creates a list of dictionaries called ‘data.’ It then filters this list, retaining dictionaries where the ‘age’ key is greater than 25, resulting in a new list named ‘filtered_data’.

Python3




data = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 22}]
filtered_data = [d for d in data if d['age'] > 25]
print(filtered_data)


Output

[{'name': 'Bob', 'age': 30}]

Filter List Of Dictionaries Based On Key Values Using filter() Function

In this example, below code uses the `filter` function and a lambda function to create a new list, ‘filtered_data,’ containing dictionaries from the original ‘data’ list where the ‘age’ key is greater than 25.

Python3




data = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 22}]
filtered_data = list(filter(lambda d: d['age'] > 25, data))
print(filtered_data)


Output

[{'name': 'Bob', 'age': 30}]

Filter List Of Dictionaries Based Using Dict Comprehension

In this example, below code utilizes a dict comprehension to create a new dictionary, ‘filtered_data,’ by extracting key-value pairs from the original ‘data’ list of dictionaries where the ‘age’ key is greater than 25.

Python3




data = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 22}]
filtered_data = {d['name']: d['age'] for d in data if d['age'] > 25}
print(filtered_data)


Output

{'Bob': 30}

Filter List Of Dictionaries Based On Key Values Using Pandas Library

In this example, in below code the Pandas library is used to convert the list of dictionaries, ‘data,’ into a DataFrame (‘df’). The DataFrame is then filtered to retain rows where the ‘age’ column is greater than 25. The resulting filtered data is converted back to a list of dictionaries using the ‘to_dict’ method .

Python3




import pandas as pd
 
data = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 22}]
df = pd.DataFrame(data)
filtered_data = df.loc[df['age'] > 25].to_dict('records')
print(filtered_data)


Output

[{'name': 'Bob', 'age': 30}]

Conclusion

In conclusion, filtering a list of dictionaries based on key values is a fundamental operation in Python programming, often encountered in various data processing and analysis tasks. The methods discussed in this article offer diverse approaches, each catering to specific needs and preferences.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads