Get Unique Values from List of Dictionary
Last Updated :
04 Feb, 2025
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': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Alice', 'age': 22}] then we want to extract the unique values (like Alice, Bob, 25, 30, 22).
Using a Set
set is a built-in Python data structure that only allows unique elements and we can use it to collect unique values from a list of dictionaries by iterating over the dictionaries and adding the values to the set.
Python
data = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Alice', 'age': 22}]
# Using a set to collect unique values
res = set(val for d in data for val in d.values())
print(res)
Output{'Alice', 22, 30, 25, 'Bob'}
Explanation:
- This code uses a generator expression val for d in data for val in d.values() to iterate over each dictionary in the list (data), then over each value within the dictionary.
- The values are added to a set which ensures that only unique values are stored automatically removing any duplicates hence the result will be a set of unique values: {'Alice', 25, 30, 22, 'Bob'}.
itertools.chain() function is a great tool for flattening lists of dictionaries. By chaining the values of all dictionaries together we can then pass them into a set to ensure uniqueness, this method is effective for large datasets because itertools.chain() creates an iterator and doesn't require the entire list to be stored in memory.
Python
from itertools import chain
data = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Alice', 'age': 22}]
# Flattening the list and using a set to get unique values
res = set(chain.from_iterable(d.values() for d in data))
print(res)
Output{'Alice', 22, 'Bob', 25, 30}
Explanation:
- chain.from_iterable() function is used to flatten the list of dictionaries by iterating over the values of each dictionary as it essentially "chains" all the values into a single iterable then result is then passed into a set to remove any duplicates ensuring that only unique values are retained.
Using Dictionary Keys
A we know that dictionaries automatically enforce uniqueness for keys we can take advantage of this property by converting the values from the list of dictionaries into dictionary keys, this guarantees that only unique values are retained.
Python
data = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Alice', 'age': 22}]
# Using dictionary keys to collect unique values
res = list(dict.fromkeys(val for d in data for val in d.values()))
print(res)
Output['Alice', 25, 'Bob', 30, 22]
Explanation:
- dict.fromkeys() method creates a dictionary where each value from the list of dictionaries is used as a key since dictionary keys are unique any duplicates are automatically removed.
- After generating the dictionary we convert it back to a list using list() which gives us the unique values: ['Alice', 25, 30, 22, 'Bob'].
Using a List
Another approach to collect unique values from a list of dictionaries is by using a list and manually checking if a value already exists before appending it while this method works, it can be less efficient than using a set due to the additional membership check for each value.
Python
data = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Alice', 'age': 22}]
# Using a list and checking for uniqueness
res = []
for d in data:
for val in d.values():
if val not in res:
res.append(val)
print(res)
Output['Alice', 25, 'Bob', 30, 22]
Explanation:
- The code iterates through each dictionary in the list data and then over each value within those dictionaries.
- Before adding a value to res, it checks whether the value is already present in the list. This ensures that only unique values are appended to the list.
Similar Reads
Get List of Values From Dictionary - Python We are given a dictionary and our task is to extract all the values from it and store them in a list. For example, if the dictionary is d = {'a': 1, 'b': 2, 'c': 3}, then the output would be [1, 2, 3].Using dict.values()We can use dict.values() along with the list() function to get the list. Here, t
2 min read
Python Get All Values from Nested Dictionary In this article, we will learn how we can Get all Values from Nested Dictionary in Python Programming. A nested Dictionary in Python is a dictionary that contains another dictionary as its values. Using this, we can create a nested structure where each of the key-value pairs is in the outer dictiona
5 min read
Get Values from Dictionary Using For Loop - Python In Python, dictionaries store data as key-value pairs and we are given a dictionary. Our task is to extract its values using a for loop, this approach allows us to iterate through the dictionary efficiently and retrieve only the values. For example, given d = {'a': 1, 'b': 2, 'c': 3}, we should extr
2 min read
Set from Dictionary Values - Python The task is to extract unique values from a dictionary and convert them into a set. In Python, sets are unordered collections that automatically eliminate duplicates. The goal is to extract all the values from the dictionary and store them in a set.For example, given a dictionary like d = {'A': 4, '
3 min read
Get Python Dictionary Values as List - Python We are given a dictionary where the values are lists and our task is to retrieve all the values as a single flattened list. For example, given the dictionary: d = {"a": [1, 2], "b": [3, 4], "c": [5]} the expected output is: [1, 2, 3, 4, 5]Using itertools.chain()itertools.chain() function efficiently
2 min read
Get Index of Values in Python Dictionary Dictionary values are lists and we might need to determine the position (or index) of each element within those lists. Since dictionaries themselves are unordered (prior to Python 3.7) or ordered based on insertion order (in Python 3.7+), the concept of "index" applies to the valuesâspecifically whe
3 min read