Python Extract Unique values dictionary values (original) (raw)
In this article, we explain several efficient methods to extract unique values from the values of a dictionary in Python.
**Example:
**Input: data = {'gfg' : [5,6,7,8],' is' : [10,11,7,5], 'best' : [6,12,10,8], 'for' : [1,2,5]}
**Output: [1, 2, 5, 6, 7, 8, 10, 11, 12]
Using set() + sum()
Flatten all lists using sum() and eliminate duplicates using set(). It’s a quick, one-line method ideal for small-to-medium datasets.
Python `
d = {'gfg' : [5,6,7,8],' is' : [10,11,7,5], 'best' : [6,12,10,8], 'for' : [1,2,5]} res = list(set(sum(d.values(), []))) print(res)
`
Output
[1, 2, 5, 6, 7, 8, 10, 11, 12]
**Explanation:
- **data.values(): fetches all value lists.
- **sum(..., []): flattens nested lists by concatenation.
- **set(): removes duplicate elements.
- **list(): converts the set into a list.
Using set comprehension + values() + sorted()
This method uses set comprehension for compact and efficient flattening of nested lists, followed by sorting for ordered output.
Python `
d = {'gfg' : [5,6,7,8],' is' : [10,11,7,5], 'best' : [6,12,10,8], 'for' : [1,2,5]} res = list(sorted({ele for val in d.values() for ele in val})) print(res)
`
Output
[1, 2, 5, 6, 7, 8, 10, 11, 12]
**Explanation:
- ****{x for v in data.values() for x in v}:** nested set comprehension flattens and removes duplicates.
- **sorted(): returns a sorted list of unique values.
Using chain() + set() + sorted()
This approach avoids intermediate lists, making it memory-efficient. chain() directly combines all lists into one sequence.
Python `
from itertools import chain d = {'gfg' : [5,6,7,8],' is' : [10,11,7,5], 'best' : [6,12,10,8], 'for' : [1,2,5]} res = list(sorted(set(chain(*d.values())))) print(res)
`
Output
[1, 2, 5, 6, 7, 8, 10, 11, 12]
**Explanation: chain(*data.values()): flattens all value lists without creating intermediate lists.
Using Counter() + append() + sort()
Counter() counts how many times each element appears, and its keys give the unique values directly.
Python `
from collections import Counter
d = {'gfg' : [5,6,7,8],' is' : [10,11,7,5], 'best' : [6,12,10,8], 'for' : [1,2,5]}
vals = [x for v in d.values() for x in v] freq = Counter(vals) res = sorted(list(freq.keys())) print(res)
`
Output
[1, 2, 5, 6, 7, 8, 10, 11, 12]
**Explanation:
- **[x for v in d.values() for x in v]: flattens all values.
- **Counter(vals): counts frequency of each element.
- **freq.keys(): retrieves unique elements.
Using extend() + if not in + sort()
This method creates a unique list manually by using extend() to combine lists and adding elements only if they aren’t already present.
Python `
d = {'gfg' : [5,6,7,8],' is' : [10,11,7,5], 'best' : [6,12,10,8], 'for' : [1,2,5]}
y, res = [], [] for v in d.values(): y.extend(v) for x in y: if x not in res: res.append(x) res.sort() print(res)
`
Output
[1, 2, 5, 6, 7, 8, 10, 11, 12]
**Explanation: extend(): merges all lists into one.
Using operator.countOf() + extend() + sort()
This method manually checks for duplicates using countOf() instead of direct membership testing to ensure each element is added only once.
Python `
import operator as op
d = {'gfg' : [5,6,7,8],' is' : [10,11,7,5], 'best' : [6,12,10,8], 'for' : [1,2,5]}
y, res = [], [] for v in d.values(): y.extend(v) for x in y: if op.countOf(res, x) == 0: res.append(x) res.sort() print(res)
`