Filter List of Python Dictionaries by Key in Python
Last Updated :
26 Feb, 2024
Filtering a list of dictionaries in Python based on a specific key is a common task in data manipulation and analysis. Whether you’re working with datasets or dictionaries, the ability to extract relevant information efficiently is crucial. In this article, we will explore some simple and commonly used methods to filter a list of dictionaries in Python by a specific key.
Filter List Of Dictionaries Python By Key In Python
Below, are the methods for Filter List Of Dictionaries Python By Key In Python.
Filter List Of Dictionaries Python By Key Using For Loop
In this approach, a for loop iterates through each dictionary in list_of_dicts. If the specified target_key exists in a dictionary (if target_key in d), that dictionary is appended to the filtered_list. This approach achieves the filtering based on the presence of the key using a simple for loop.
Python3
list_of_dicts = [{ 'subject' : 'Art' , 'marks' : 25 }, { 'subject' : 'biology' }, { 'marks' : 30 }]
target_key = 'subject'
filtered_list = []
for d in list_of_dicts:
if target_key in d:
filtered_list.append(d)
print ( "Filtered List:" , filtered_list)
|
Output
Filtered List: [{'subject': 'Art', 'marks': 25}, {'subject': 'biology'}]
Filter List Of Dictionaries Python By Key Using List Comprehension
This method involves using a list comprehension to filter dictionaries based on the existence of a specified key.
Python3
list_of_dicts = [{ 'name' : 'A' , 'class' : 5 }, { 'name' : 'B' }, { 'class' : 10 }]
target_key = 'name'
filtered_list = [d for d in list_of_dicts if target_key in d]
print ( "Filtered List :" , filtered_list)
|
Output
Filtered List : [{'name': 'A', 'class': 5}, {'name': 'B'}]
Filter List Of Dictionaries Python By Key Using Filter()
Function
The filter
function, combined with a lambda
function, allows us to selectively include dictionaries based on the specified key.
Python3
list_of_dicts = [{ 'name' : 'Alice' , 'age' : 25 }, { 'name' : 'Bob' }, { 'age' : 30 }]
target_key = 'name'
filtered_list = list ( filter ( lambda d: target_key in d, list_of_dicts))
print ( "Filtered List :" , filtered_list)
|
Output
Filtered List : [{'name': 'Alice', 'age': 25}, {'name': 'Bob'}]
Filter List Of Dictionaries Python By Key Using map() Function
In this approach, map
is used to apply a lambda function to each dictionary in list_of_dicts
. The lambda function checks if the target_key
exists in a dictionary, and if it does, the dictionary is returned; otherwise, None
is returned. The final list comprehension is then used to filter out the None
values, resulting in the filtered list.
Python3
list_of_dicts = [{ 'subject' : 'Art' , 'marks' : 25 }, { 'subject' : 'biology' }, { 'marks' : 30 }]
target_key = 'subject'
filtered_list = list ( map ( lambda d: d if target_key in d else None , list_of_dicts))
filtered_list = [d for d in filtered_list if d is not None ]
print ( "Filtered List:" , filtered_list)
|
Output
Filtered List: [{'subject': 'Art', 'marks': 25}, {'subject': 'biology'}]
Conclusion
In this article we came to understand how to do filtering of a list of dictionaries by a specific key in Python . This can be achieved through various methods and we can decide which one to use by complexity of the filtering criteria and the desired output format. List comprehensions, lambda functions and many more provide flexible and efficient solutions for different scenarios.
Please Login to comment...