
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Remove Dictionaries with Matching Values
When it is required to remove dictionaries with matching values, a dictionary comprehension is used.
Below is a demonstration of the same −
Example
my_dict_1 = [{'Hi': 32, "there": 32, "Will":19},{'Hi': 19, "there": 100, "Will": 13}, {'Hi': 72, "there": 19, "Will": 72}] print("The first dictionary is : ") print(my_dict_1) my_dict_2 = [{'Hi': 72, "Will": 19}, {"Will": 13, "Hi": 19}] print("The second dictionary is : ") print(my_dict_2) K = "Hi" print("The value of K is ") print(K) temp = { element[K] for element in my_dict_2} my_result = [element for element in my_dict_1 if element[K] not in temp] print("The result is : " ) print(my_result)
Output
The first dictionary is : [{'Hi': 32, 'there': 32, 'Will': 19}, {'Hi': 19, 'there': 100, 'Will': 13}, {'Hi': 72, 'there': 19, 'Will': 72}] The second dictionary is : [{'Hi': 72, 'Will': 19}, {'Will': 13, 'Hi': 19}] The value of K is Hi The result is : [{'Hi': 32, 'there': 32, 'Will': 19}]
Explanation
Two dictionaries are defined and is displayed on the console.
A value for K is defined and displayed on console.
The second dictionary is iterated over, and elements are checked with K and stored in a temporary variable ‘temp’.
The first dictionary is iterated over, and elements in it are checked with the temporary variable ‘temp’ and assigned to a variable.
This result is assigned to a variable.
This is the output that is displayed on the console.
- Related Articles
- Python – Filter dictionaries with ordered values
- Python – Extract dictionaries with values sum greater than K
- Remove matching tuples in Python
- Python – Dictionaries with Unique Value Lists
- Matching odd even indices with values in JavaScript
- Ways to sort list of dictionaries by values in Python
- Ways to sort list of dictionaries using values in python
- Python - Filter dictionaries by values in Kth Key in list
- Python – Merge Dictionaries List with duplicate Keys
- Pattern matching in Python with Regex
- How to perform Calculations with Dictionaries in Python?
- Python - Remove a column with all null values in Pandas
- Ways to sort list of dictionaries by values in Python Using itemgetter
- How to sort a list of dictionaries by values of dictionaries in C#?
- Ways to sort list of dictionaries by values in Python Using lambda function

Advertisements