Unique values from list values

EasyDictionary Programs · 80 of 94

Write a Python program to extract the unique values from all list values, sorted.

Advertisement
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

  1. A set comprehension, then sort.

Solution

Python
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]
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs