Python Practice › Dictionary Programs › Program 80
Unique values from list values
Write a Python program to extract the unique values from all list values, sorted.
Input
{"sqa": [1, 2, 5, 6], "tools": [3, 8, 9], "is": [2, 5, 0], "best": [3, 6, 8]}Output
[0, 1, 2, 3, 5, 6, 8, 9]
Approach
- A set comprehension, then sort.
Solution
d = {"sqa": [1, 2, 5, 6], "tools": [3, 8, 9], "is": [2, 5, 0], "best": [3, 6, 8]}print(sorted({x for v in d.values() for x in v}))Output
[0, 1, 2, 3, 5, 6, 8, 9]
Learn the concept
New to this topic? Read the lesson, then come back.