Unique Dictionary Filter in List - Python
Last Updated :
13 Feb, 2025
We are given a dictionary in list we need to find unique dictionary. For example, a = [ {"a": 1, "b": 2}, {"a": 1, "b": 2}, {"c": 3}, {"a": 1, "b": 3}] so that output should be [{'a': 1, 'b': 2}, {'a': 1, 'b': 3}, {'c': 3}].
Using set
with frozenset
Using set
with frozenset,
we convert dictionary items to immutable sets allowing them to be added to a set for uniqueness filtering. This method ensures only unique dictionaries are retained in list.
Python
a = [
{"a": 1, "b": 2},
{"a": 1, "b": 2},
{"c": 3},
{"a": 1, "b": 3}
]
# Convert each dictionary to a frozenset of items, ensuring hashable representation
u = [dict(d) for d in {frozenset(d.items()) for d in a}]
print(u)
Output[{'c': 3}, {'b': 3, 'a': 1}, {'a': 1, 'b': 2}]
Explanation:
- Each dictionary is converted to a frozenset of its key-value pairs, creating a hashable representation that allows duplicates to be removed using a set.
- Unique frozenset items are converted back to dictionaries and result is stored in u ensuring list contains only unique dictionaries.
Using json.dumps
json.dumps converts dictionaries to JSON strings providing a hashable and comparable format. By storing these strings in a set duplicates can be filtered and unique strings are then converted back to dictionaries.
Python
import json
a = [
{"a": 1, "b": 2},
{"a": 1, "b": 2},
{"c": 3},
{"a": 1, "b": 3}
]
seen = set()
u = []
for d in a:
# Convert dictionary to a JSON string with sorted keys
di = json.dumps(d, sort_keys=True)
# Add dictionary to result if not seen before
if di not in seen:
u.append(d)
seen.add(di)
print(u)
Output[{'a': 1, 'b': 2}, {'c': 3}, {'a': 1, 'b': 3}]
Explanation:
- Each dictionary is converted to a JSON string with sorted keys using json.dumps ensuring consistent ordering for comparison.
- A set seen tracks JSON strings to filter out duplicates and unique dictionaries are appended to list u which is then printed.
Using all()
all() checks if all conditions in an iterable are True. It can be used to compare each dictionary with those already added to a result list ensuring no duplicates before appending.
Python
a = [
{"a": 1, "b": 2},
{"a": 1, "b": 2},
{"c": 3},
{"a": 1, "b": 3}
]
u = []
for d in a:
# Add dictionary if it is not equal to any existing dictionary in the list
if all(d != existing for existing in u):
u.append(d)
print(u)
Output[{'a': 1, 'b': 2}, {'c': 3}, {'a': 1, 'b': 3}]
Explanation:
- For each dictionary d in the list all() ensures it is not equal to any dictionary already in the result list u.
- If d is unique it is appended to u resulting in a list of unique dictionaries which is then printed.
Similar Reads
Filter List Of Dictionaries in Python Filtering a list of dictionaries is a fundamental programming task that involves selecting specific elements from a collection of dictionaries based on defined criteria. This process is commonly used for data manipulation and extraction, allowing developers to efficiently work with structured data b
2 min read
Python | Filter Tuple Dictionary Keys Sometimes, while working with Python dictionaries, we can have itâs keys in form of tuples. A tuple can have many elements in it and sometimes, it can be essential to get them. If they are a part of a dictionary keys and we desire to get filtered tuple key elements, we need to perform certain functi
4 min read
Python - Dictionaries with Unique Value Lists Given List of dictionaries with list values, extract unique dictionaries. Input : [{'Gfg': [2, 3], 'is' : [7, 8], 'best' : [10]}, {'Gfg': [2, 3], 'is' : [7, 8], 'best' : [10]}] Output : [{'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}] Explanation : Both are similar dictionaries, and hence 1 is removed.
4 min read
Filter List of Python Dictionaries by Key in Python In Python, filtering a list of dictionaries based on a specific key is a common task when working with structured data. In this article, weâll explore different ways to filter a list of dictionaries by key from the most efficient to the least.Using List Comprehension List comprehension is a concise
3 min read
Filtering a List of Dictionary on Multiple Values in Python Filtering a list of dictionaries is a common task in programming, especially when dealing with datasets. Often, you may need to extract specific elements that meet certain criteria. In this article, we'll explore four generally used methods for filtering a list of dictionaries based on multiple valu
4 min read