Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Filter dictionaries by values in Kth Key in list
When working with a list of dictionaries, you might need to filter them based on whether a specific key's value appears in a given list. This can be accomplished using simple iteration or more Pythonic approaches like list comprehension.
Using For Loop Method
The basic approach iterates through each dictionary and checks if the target key's value exists in the search list ?
data_list = [{"Python": 2, "is": 4, "cool": 11},
{"Python": 5, "is": 1, "cool": 1},
{"Python": 7, "is": 3, "cool": 7},
{"Python": 9, "is": 9, "cool": 8},
{"Python": 4, "is": 10, "cool": 6}]
print("The original list:")
print(data_list)
search_values = [1, 9, 8, 4, 5]
target_key = "is"
filtered_result = []
for dictionary in data_list:
if dictionary[target_key] in search_values:
filtered_result.append(dictionary)
print(f"\nFiltered dictionaries where '{target_key}' value is in {search_values}:")
print(filtered_result)
The original list:
[{'Python': 2, 'is': 4, 'cool': 11}, {'Python': 5, 'is': 1, 'cool': 1}, {'Python': 7, 'is': 3, 'cool': 7}, {'Python': 9, 'is': 9, 'cool': 8}, {'Python': 4, 'is': 10, 'cool': 6}]
Filtered dictionaries where 'is' value is in [1, 9, 8, 4, 5]:
[{'Python': 2, 'is': 4, 'cool': 11}, {'Python': 5, 'is': 1, 'cool': 1}, {'Python': 9, 'is': 9, 'cool': 8}]
Using List Comprehension
A more concise approach using list comprehension for the same filtering operation ?
data_list = [{"Python": 2, "is": 4, "cool": 11},
{"Python": 5, "is": 1, "cool": 1},
{"Python": 7, "is": 3, "cool": 7},
{"Python": 9, "is": 9, "cool": 8},
{"Python": 4, "is": 10, "cool": 6}]
search_values = [1, 9, 8, 4, 5]
target_key = "is"
# List comprehension approach
filtered_result = [d for d in data_list if d[target_key] in search_values]
print("Filtered result using list comprehension:")
print(filtered_result)
Filtered result using list comprehension:
[{'Python': 2, 'is': 4, 'cool': 11}, {'Python': 5, 'is': 1, 'cool': 1}, {'Python': 9, 'is': 9, 'cool': 8}]
Using Filter Function
Using the built-in filter() function with a lambda expression ?
data_list = [{"Python": 2, "is": 4, "cool": 11},
{"Python": 5, "is": 1, "cool": 1},
{"Python": 7, "is": 3, "cool": 7},
{"Python": 9, "is": 9, "cool": 8},
{"Python": 4, "is": 10, "cool": 6}]
search_values = [1, 9, 8, 4, 5]
target_key = "is"
# Using filter function
filtered_result = list(filter(lambda d: d[target_key] in search_values, data_list))
print("Filtered result using filter function:")
print(filtered_result)
Filtered result using filter function:
[{'Python': 2, 'is': 4, 'cool': 11}, {'Python': 5, 'is': 1, 'cool': 1}, {'Python': 9, 'is': 9, 'cool': 8}]
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| For Loop | High | Good | Complex logic, beginners |
| List Comprehension | High | Best | Simple conditions, Pythonic code |
| Filter Function | Medium | Good | Functional programming style |
Conclusion
List comprehension provides the most Pythonic and efficient way to filter dictionaries by key values. Use for loops when you need more complex filtering logic or when working with beginners who prefer explicit iteration.
