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
Selected Reading
Python – Remove Dictionaries with Matching Values
When working with lists of dictionaries, you may need to remove dictionaries that have matching values for a specific key. Python provides several approaches using dictionary comprehensions and set operations.
Basic Example
Here's how to remove dictionaries from one list that have matching values with dictionaries in another list ?
# List of dictionaries to filter
dict_list_1 = [
{'Hi': 32, "there": 32, "Will": 19},
{'Hi': 19, "there": 100, "Will": 13},
{'Hi': 72, "there": 19, "Will": 72}
]
print("The first dictionary list is:")
print(dict_list_1)
# List of dictionaries with values to match against
dict_list_2 = [
{'Hi': 72, "Will": 19},
{"Will": 13, "Hi": 19}
]
print("The second dictionary list is:")
print(dict_list_2)
# Key to check for matching values
key = "Hi"
print(f"Checking key: {key}")
# Extract values from second list for the specified key
values_to_exclude = {element[key] for element in dict_list_2}
# Filter first list to exclude dictionaries with matching values
result = [element for element in dict_list_1 if element[key] not in values_to_exclude]
print("The result is:")
print(result)
The first dictionary list is:
[{'Hi': 32, 'there': 32, 'Will': 19}, {'Hi': 19, 'there': 100, 'Will': 13}, {'Hi': 72, 'there': 19, 'Will': 72}]
The second dictionary list is:
[{'Hi': 72, 'Will': 19}, {'Will': 13, 'Hi': 19}]
Checking key: Hi
The result is:
[{'Hi': 32, 'there': 32, 'Will': 19}]
How It Works
The solution uses a two-step approach:
- Extract values: Create a set of values from the second list for the specified key
- Filter dictionaries: Use list comprehension to keep only dictionaries whose key value is not in the exclusion set
Alternative Method Using Functions
For reusability, you can create a function to handle this operation ?
def remove_matching_dicts(source_list, exclude_list, key):
"""Remove dictionaries with matching values for a specific key"""
exclude_values = {d[key] for d in exclude_list if key in d}
return [d for d in source_list if key in d and d[key] not in exclude_values]
# Example usage
products = [
{'id': 1, 'name': 'Laptop', 'price': 800},
{'id': 2, 'name': 'Mouse', 'price': 25},
{'id': 3, 'name': 'Keyboard', 'price': 50}
]
discontinued = [
{'id': 2, 'status': 'discontinued'},
{'id': 5, 'status': 'discontinued'}
]
active_products = remove_matching_dicts(products, discontinued, 'id')
print("Active products:")
print(active_products)
Active products:
[{'id': 1, 'name': 'Laptop', 'price': 800}, {'id': 3, 'name': 'Keyboard', 'price': 50}]
Key Points
- Set comprehension
{element[key] for element in dict_list}creates a set of values for efficient lookup - List comprehension filters dictionaries based on the exclusion criteria
- The
not inoperator checks if a value exists in the exclusion set - This approach works with any hashable data type as dictionary values
Conclusion
Use set comprehension with list comprehension to efficiently remove dictionaries with matching values. This approach provides O(1) lookup time for value checking and creates clean, readable code for filtering operations.
Advertisements
