Python - Dictionaries with Unique Value Lists
Last Updated :
13 Apr, 2023
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.
Input : [{'Gfg': [2, 3], 'is' : [7, 8], 'best' : [10]}, {'Gfg': [2, 3], 'is' : [7, 8], 'best' : [10, 11]}]
Output : [{'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}, {'Gfg': [2, 3], 'is': [7, 8], 'best': [10, 11]}]
Explanation : None duplicate.
Method #1 : Using loop
This is one of the ways in which this task can be performed. In this, we iterate for each dictionary and memoize it, and prevent it from adding to result.
Python3
# Python3 code to demonstrate working of
# Unique Value Lists Dictionaries
# Using loop
# initializing lists
test_list = [{'Gfg': [2, 3], 'is' : [7, 8], 'best' : [10]},
{'Gfg': [2, 3], 'is' : [7], 'best' : [10]},
{'Gfg': [2, 3], 'is' : [7, 8], 'best' : [10]}]
# printing original list
print("The original list : " + str(test_list))
# Using loop to iterate through elements
# result array to also keep track of already occurred elements
res = []
for sub in test_list:
if sub not in res:
res.append(sub)
# printing result
print("List after duplicates removal : " + str(res))
OutputThe original list : [{'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}, {'Gfg': [2, 3], 'is': [7], 'best': [10]}, {'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}]
List after duplicates removal : [{'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}, {'Gfg': [2, 3], 'is': [7], 'best': [10]}]
Time complexity: O(n), where n is the length of the test_list. The loop takes O(n) time
Auxiliary Space: O(n), extra space of size n is required
Method #2 : Using list comprehension
This is yet another way in which this task can be performed. In this, similar approach is employed as above, just the difference of encapsulating result in list comprehension for one-liner.
Python3
# Python3 code to demonstrate working of
# Unique Value Lists Dictionaries
# Using list comprehension
# initializing lists
test_list = [{'Gfg': [2, 3], 'is' : [7, 8], 'best' : [10]},
{'Gfg': [2, 3], 'is' : [7], 'best' : [10]},
{'Gfg': [2, 3], 'is' : [7, 8], 'best' : [10]}]
# printing original list
print("The original list : " + str(test_list))
# list comprehension to encapsulate logic in one liner
res = []
[res.append(val) for val in test_list if val not in res]
# printing result
print("List after duplicates removal : " + str(res))
OutputThe original list : [{'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}, {'Gfg': [2, 3], 'is': [7], 'best': [10]}, {'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}]
List after duplicates removal : [{'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}, {'Gfg': [2, 3], 'is': [7], 'best': [10]}]
Method 3: Using a set and tuple.
Step-by-step approach:
- Create an empty set to hold the unique values.
- Loop through each dictionary in the list.
- Convert the dictionary to a tuple, since sets can't contain dictionaries.
- Check if the tuple is in the set of unique values. If it's not, add it to the set and append the original dictionary to the result list.
- Print the result list.
Python3
# Python3 code to demonstrate working of
# Unique Value Lists Dictionaries
# Using set and tuple
# initializing lists
test_list = [{'Gfg': (2, 3), 'is' : (7, 8), 'best' : (10,)},
{'Gfg': (2, 3), 'is' : (7,), 'best' : (10,)},
{'Gfg': (2, 3), 'is' : (7, 8), 'best' : (10,)}]
# printing original list
print("The original list : " + str(test_list))
# set to hold unique values
unique_set = set()
# result list
res = []
# loop through each dictionary
for d in test_list:
# convert dictionary to tuple
d_tuple = tuple(sorted(d.items()))
# check if tuple is in unique set
if d_tuple not in unique_set:
# if not, add tuple to set and original dictionary to result list
unique_set.add(d_tuple)
res.append(d)
# printing result
print("List after duplicates removal : " + str(res))
OutputThe original list : [{'Gfg': (2, 3), 'is': (7, 8), 'best': (10,)}, {'Gfg': (2, 3), 'is': (7,), 'best': (10,)}, {'Gfg': (2, 3), 'is': (7, 8), 'best': (10,)}]
List after duplicates removal : [{'Gfg': (2, 3), 'is': (7, 8), 'best': (10,)}, {'Gfg': (2, 3), 'is': (7,), 'best': (10,)}]
Time complexity: O(n log n) due to sorting the dictionaries into tuples.
Auxiliary space: O(n) for storing the unique values in a set and the result list.
Similar Reads
Python - Unique Values of Key in Dictionary
We are given a list of dictionaries and our task is to extract all the unique values associated with a given key. For example, consider: data = [ {"name": "Aryan", "age": 25}, {"name": "Harsh", "age": 30}, {"name": "Kunal", "age": 22}, {"name": "Aryan", "age": 27}]key = "name"Then, the unique values
4 min read
Inverse Dictionary Values List - Python
We are given a dictionary and the task is to create a new dictionary where each element of the value lists becomes a key and the original keys are grouped as lists of values for these new keys.For example: dict = {1: [2, 3], 2: [3], 3: [1]} then output will be {2: [1], 3: [1, 2], 1: [3]}Using defaul
2 min read
Python | Sum list of dictionaries with same key
You have given a list of dictionaries, the task is to return a single dictionary with sum values with the same key. Let's discuss different methods to do the task. Method #1: Using reduce() + operator Step-by-step approach: Import necessary modules - collections, functools, and operator.Initialize a
7 min read
Python - Update values of a list of dictionaries
The task of updating the values of a list of dictionaries in Python involves modifying specific keys or values within each dictionary in the list based on given criteria or conditions. This task is commonly encountered when working with structured data that needs transformation or enrichment.For exa
4 min read
Unique Dictionary Filter in List - Python
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 frozensetUsing set with frozenset, we convert dictionary item
3 min read
Python | Iterate through value lists dictionary
While working with dictionary, we can have a case in which we need to iterate through the lists, which are in the keys of dictionaries. This kind of problem can occur in web development domain. Let's discuss certain ways in which this problem can be solved. Method #1: Using list comprehension List c
4 min read
Python - Create a Dictionary using List with None Values
The task of creating a dictionary from a list of keys in Python involves transforming a list of elements into a dictionary where each element becomes a key. Each key is typically assigned a default value, such as None, which can be updated later. For example, if we have a list like ["A", "B", "C"],
3 min read
Python - Dictionary with Index as Value
We are having a list we need to find index of each element and store it in form of dictionary. For example, a = ['a', 'b', 'c', 'd'] we need to find index of each elements in list so that output should be {'a': 0, 'b': 1, 'c': 2, 'd': 3}.Using Dictionary ComprehensionWe can use dictionary comprehens
2 min read
Python - Initialize dictionary with custom value list
In python one usually comes across situations in which one has to use dictionary for storing the lists. But in those cases, one usually checks for first element and then creates a list corresponding to key when it comes. But its always wanted a method to initialize the dict. keys with a custom list.
4 min read
Get Unique Values from List of Dictionary
We are given a list of dictionaries and our task is to extract the unique values from these dictionaries, this is common when dealing with large datasets or when you need to find unique information from multiple records. For example, if we have the following list of dictionaries: data = [{'name': 'A
4 min read