Filter List of Python Dictionaries by Key in Python
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.
- Using For Loop
- Using List Comprehension
- Using F
ilter()
Function - Using map() Function
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 dictionaries list_of_dicts = [{ 'subject' : 'Art' , 'marks' : 25 }, { 'subject' : 'biology' }, { 'marks' : 30 }] # Specify the key for filtering (ensure case sensitivity) target_key = 'subject' # Initialize an empty list to store filtered dictionaries filtered_list = [] # Filter using a for loop for d in list_of_dicts: if target_key in d: filtered_list.append(d) # Print the result print ( "Filtered List:" , filtered_list) |
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 dictionaries list_of_dicts = [{ 'name' : 'A' , 'class' : 5 }, { 'name' : 'B' }, { 'class' : 10 }] # Specify the key for filtering target_key = 'name' # Filter using list comprehension filtered_list = [d for d in list_of_dicts if target_key in d] # Print the result print ( "Filtered List :" , filtered_list) |
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 dictionaries list_of_dicts = [{ 'name' : 'Alice' , 'age' : 25 }, { 'name' : 'Bob' }, { 'age' : 30 }] # Specify the key for filtering target_key = 'name' # Filter using filter and lambda filtered_list = list ( filter ( lambda d: target_key in d, list_of_dicts)) # Print the result print ( "Filtered List :" , filtered_list) |
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 dictionaries list_of_dicts = [{ 'subject' : 'Art' , 'marks' : 25 }, { 'subject' : 'biology' }, { 'marks' : 30 }] # Specify the key for filtering (ensure case sensitivity) target_key = 'subject' # Filter using map and lambda 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 the result print ( "Filtered List:" , filtered_list) |
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.