Filtering a List of Dictionary on Multiple Values in Python
Last Updated :
07 Feb, 2024
Filtering a list of dictionaries is a common task in programming, especially when dealing with datasets. Often, you may need to extract specific elements that meet certain criteria. In this article, we’ll explore four generally used methods for filtering a list of dictionaries based on multiple values, providing code examples in Python.
Filtering A List Of Dictionaries Based On Multiple Values
Below, are the method of Filtering A List Of Dictionaries Based On Multiple Values in Python.
Filtering A List Of Dictionaries Based On Multiple Values Using List Comprehension
List comprehension is a concise and efficient way to create lists in Python. It can be leveraged to filter a list of dictionaries based on multiple values.
Example: In this example, the code creates a list of dictionaries named ‘data’ and filters it, retaining only dictionaries where the ‘age’ is greater than 25 and the ‘city’ is ‘New York’. The resulting filtered data is stored in the ‘filtered_data’ variable and printed.
Python3
data = [
{ 'name' : 'Alice' , 'age' : 25 , 'city' : 'New York' },
{ 'name' : 'Bob' , 'age' : 30 , 'city' : 'San Francisco' },
{ 'name' : 'Charlie' , 'age' : 28 , 'city' : 'New York' },
]
filtered_data = [item for item in data if item[ 'age' ] > 25 and item[ 'city' ] = = 'New York' ]
print (filtered_data)
|
Output
[{'name': 'Charlie', 'age': 28, 'city': 'New York'}]
Filtering A List Of Dictionaries Based On Multiple Values Using Filter and Lambda Function
The filter
function in combination with a lambda function is another approach for filtering lists based on multiple criteria.
Example : In this example, The code defines a list of dictionaries called ‘data’ and uses the `filter` function with a lambda expression to create ‘filtered_data’, containing dictionaries where the ‘age’ is greater than 25 and the ‘city’ is ‘New York’.
Python3
data = [
{ 'name' : 'Alice' , 'age' : 25 , 'city' : 'New York' },
{ 'name' : 'Bob' , 'age' : 30 , 'city' : 'San Francisco' },
{ 'name' : 'Charlie' , 'age' : 28 , 'city' : 'New York' },
]
filtered_data = list ( filter ( lambda x: x[ 'age' ] > 25 and x[ 'city' ] = = 'New York' , data))
print (filtered_data)
|
Output
[{'name': 'Charlie', 'age': 28, 'city': 'New York'}]
Filtering A List Of Dictionaries Based On Multiple Values Using Pandas DataFrame
If your data is structured and large, using a Pandas DataFrame can provide a powerful solution for filtering.
Example : In this example, The code uses the Pandas library to create a DataFrame from the list of dictionaries named ‘data’. It then filters the DataFrame to include rows where the ‘age’ is greater than 25 and the ‘city’ is ‘New York’.
Python3
import pandas as pd
data = [
{ 'name' : 'Alice' , 'age' : 25 , 'city' : 'New York' },
{ 'name' : 'Bob' , 'age' : 30 , 'city' : 'San Francisco' },
{ 'name' : 'Charlie' , 'age' : 28 , 'city' : 'New York' },
]
df = pd.DataFrame(data)
filtered_data = df[(df[ 'age' ] > 25 ) & (df[ 'city' ] = = 'New York' )].to_dict( 'records' )
print (filtered_data)
|
Output
[{'name': 'Charlie', 'age': 28, 'city': 'New York'}]
Filtering A List Of Dictionaries Based On Multiple Values Using custom Function
Create a custom filtering function that accepts a dictionary and returns True
if it meets the criteria.
Example : In this example, The code defines a custom filtering function named ‘custom_filter’ that checks if the ‘age’ is greater than 25 and the ‘city’ is ‘New York’ for a given dictionary. It then applies this filter function to the list of dictionaries called ‘data’ using the `filter` function, creating ‘filtered_data’.
Python3
def custom_filter(item):
return item[ 'age' ] > 25 and item[ 'city' ] = = 'New York'
data = [
{ 'name' : 'Alice' , 'age' : 25 , 'city' : 'New York' },
{ 'name' : 'Bob' , 'age' : 30 , 'city' : 'San Francisco' },
{ 'name' : 'Charlie' , 'age' : 28 , 'city' : 'New York' },
]
filtered_data = list ( filter (custom_filter, data))
print (filtered_data)
|
Output
[{'name': 'Charlie', 'age': 28, 'city': 'New York'}]
Conclusion
Filtering a list of dictionaries based on multiple values is a common requirement in programming. The choice of method depends on the specific use case, data size, and personal preference. These four methods – list comprehension, filter with lambda function, Pandas DataFrame, and custom filtering function – provide versatile options for efficiently extracting relevant information from your data.
Share your thoughts in the comments
Please Login to comment...