5 Best Ways to Filter a Python List of Dicts by Value

Rate this post
Filtering List of Dictionaries by Value in Python

πŸ’‘ Problem Formulation: Consider you have a list of dictionaries in Python, and you want to filter this list based on the value of a specific key in the dictionaries. For instance, you might have a list of employees where each employee is a dictionary with details like name, age, and department. The task is to filter the list to include only those employees who belong to a certain department, e.g., ‘Engineering’.

Method 1: Using a for-loop

This method involves iterating over the list using a for-loop and appending dictionaries that match the filter condition to a new list. It’s straightforward and readable, but may not be the most efficient for very large lists.

Here’s an example:

employees = [
    {'name': 'Alice', 'department': 'Engineering'},
    {'name': 'Bob', 'department': 'Sales'},
    {'name': 'Charlie', 'department': 'Engineering'}
]

filtered_employees = []
for employee in employees:
    if employee['department'] == 'Engineering':
        filtered_employees.append(employee)

print(filtered_employees)

Output:

[
    {'name': 'Alice', 'department': 'Engineering'},
    {'name': 'Charlie', 'department': 'Engineering'}
]

This snippet filters out the employees in the Engineering department by looping through each dictionary in the list and adding the qualifying dictionaries to a new list called filtered_employees.

Method 2: Using List Comprehensions

List comprehensions provide a concise way to create lists. The syntax is designed to mirror the mathematical notation. This method is a more Pythonic way of filtering lists and often more efficient than using a for-loop.

Here’s an example:

filtered_employees = [emp for emp in employees if emp['department'] == 'Engineering']
print(filtered_employees)

Output:

[
    {'name': 'Alice', 'department': 'Engineering'},
    {'name': 'Charlie', 'department': 'Engineering'}
]

The code snippet creates a new filtered list of employees using a list comprehension, which is a compact and efficient way to filter items in Python.

Method 3: Using the filter() Function

The filter() function allows you to process an iterable and extract elements for which a function returns True. This method is useful when the logic for filtering is too complex for a list comprehension.

Here’s an example:

def is_engineering(employee):
    return employee['department'] == 'Engineering'

filtered_employees = list(filter(is_engineering, employees))
print(filtered_employees)

Output:

[
    {'name': 'Alice', 'department': 'Engineering'},
    {'name': 'Charlie', 'department': 'Engineering'}
]

This code leverages the filter() function with a custom function is_engineering to select dictionaries where the department key is ‘Engineering’.

Method 4: Using the functools.reduce()

This method employs the functools.reduce() function, which accumulates a result by applying a function to items of an iterable, from left to right. It is less readable but can be more performant in certain scenarios.

Here’s an example:

from functools import reduce

def filter_engineering(acc, emp):
    if emp['department'] == 'Engineering':
        acc.append(emp)
    return acc

filtered_employees = reduce(filter_engineering, employees, [])
print(filtered_employees)

Output:

[
    {'name': 'Alice', 'department': 'Engineering'},
    {'name': 'Charlie', 'department': 'Engineering'}
]

The reduce() function takes a function and an iterable, using an accumulator to build up a result. In this case, it’s a filtered list of employees by department.

Bonus One-Liner Method 5: Using a Lambda Expression with filter()

Lambda expressions provide a way to create small anonymous functions. When combined with filter(), you can filter a list succinctly inline without defining a separate function.

Here’s an example:

filtered_employees = list(filter(lambda emp: emp['department'] == 'Engineering', employees))
print(filtered_employees)

Output:

[
    {'name': 'Alice', 'department': 'Engineering'},
    {'name': 'Charlie', 'department': 'Engineering'}
]

Here, a lambda function is used in conjunction with filter() to achieve the same result as method 3, but without the need for a separate function definition.

Summary/Discussion

  • Method 1: For-loop. Straightforward and readable. Not as efficient for large datasets.
  • Method 2: List Comprehensions. Pythonic and more efficient than for-loops. Can become less readable with complex logic.
  • Method 3: filter() Function. Clean and functional approach. Requires additional function definition which can be verbose for simple cases.
  • Method 4: functools.reduce(). Powerful for accumulation tasks. Can be less intuitive and harder to read, especially for those not familiar with functional programming concepts.
  • Method 5: Lambda with filter(). Compact one-liner. Can be harder to read and debug for complex filtering logic.