Unique values in a list of dicts
Write a Python program to print the unique values from a list of dictionaries.
[{"name1": "robert"}, {"name2": "john"}, {"name3": "jim"}, {"name4": "robert"}]['robert', 'john', 'jim']
Approach
- Collect values in order and remove duplicates with
dict.fromkeys.
Solution
data = [{"name1": "robert"}, {"name2": "john"}, {"name3": "jim"}, {"name4": "robert"}]values = [v for d in data for v in d.values()]print(list(dict.fromkeys(values)))['robert', 'john', 'jim']
Learn the concept
New to this topic? Read the lesson, then come back.