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 dictionary key based on the values in selective list
Sometimes in a Python dictionary we may need to filter out certain keys based on a selective list. This allows us to extract specific key-value pairs from a larger dictionary. In this article we will see how to filter dictionary keys using different approaches.
Using List Comprehension with 'in' Operator
In this approach we put the keys to be filtered in a list. Then iterate through each element of the list and check for its presence in the given dictionary. We create a resulting dictionary containing only these filtered key-value pairs ?
Example
# Original dictionary
dictA = {'Mon': 'Phy', 'Tue': 'chem', 'Wed': 'Math', 'Thu': 'Bio'}
key_list = ['Tue', 'Thu']
print("Given Dictionary:")
print(dictA)
print("Keys for filter:")
print(key_list)
# Filter dictionary to get key-value pairs
filtered_dict = {key: dictA[key] for key in key_list if key in dictA}
print("Filtered dictionary:")
print(filtered_dict)
# If you only want the values
values_only = [dictA[key] for key in key_list if key in dictA]
print("Values only:")
print(values_only)
Given Dictionary:
{'Mon': 'Phy', 'Tue': 'chem', 'Wed': 'Math', 'Thu': 'Bio'}
Keys for filter:
['Tue', 'Thu']
Filtered dictionary:
{'Tue': 'chem', 'Thu': 'Bio'}
Values only:
['chem', 'Bio']
Using Set Intersection
We use intersection to find the common keys between the given dictionary and the filter list. This approach is more efficient when dealing with large dictionaries as set operations are optimized ?
Example
# Original dictionary
dictA = {'Mon': 'Phy', 'Tue': 'chem', 'Wed': 'Math', 'Thu': 'Bio'}
key_list = ['Tue', 'Thu', 'Fri'] # Note: 'Fri' doesn't exist in dictA
print("Given Dictionary:")
print(dictA)
print("Keys for filter:")
print(key_list)
# Find intersection of keys
common_keys = list(set(key_list).intersection(dictA.keys()))
print("Common keys found:")
print(common_keys)
# Create filtered dictionary
filtered_dict = {key: dictA[key] for key in common_keys}
print("Filtered dictionary:")
print(filtered_dict)
Given Dictionary:
{'Mon': 'Phy', 'Tue': 'chem', 'Wed': 'Math', 'Thu': 'Bio'}
Keys for filter:
['Tue', 'Thu', 'Fri']
Common keys found:
['Thu', 'Tue']
Filtered dictionary:
{'Thu': 'Bio', 'Tue': 'chem'}
Using Dictionary Comprehension with filter()
This approach uses the built-in filter() function combined with dictionary comprehension for a more functional programming style ?
Example
# Original dictionary
dictA = {'Mon': 'Phy', 'Tue': 'chem', 'Wed': 'Math', 'Thu': 'Bio'}
key_list = ['Tue', 'Thu']
print("Given Dictionary:")
print(dictA)
print("Keys for filter:")
print(key_list)
# Using filter() function
filtered_keys = filter(lambda x: x in dictA, key_list)
filtered_dict = {key: dictA[key] for key in filtered_keys}
print("Filtered dictionary:")
print(filtered_dict)
Given Dictionary:
{'Mon': 'Phy', 'Tue': 'chem', 'Wed': 'Math', 'Thu': 'Bio'}
Keys for filter:
['Tue', 'Thu']
Filtered dictionary:
{'Tue': 'chem', 'Thu': 'Bio'}
Comparison
| Method | Best For | Time Complexity | Maintains Order |
|---|---|---|---|
| List Comprehension | Simple filtering | O(n) | Yes |
| Set Intersection | Large dictionaries | O(min(m,n)) | No |
| filter() Function | Functional style | O(n) | Yes |
Conclusion
Use list comprehension for simple key filtering when order matters. Use set intersection for better performance with large datasets. Choose the method that best fits your specific use case and coding style preferences.
