Unique values in a list of dicts

EasyDictionary Programs · 33 of 94

Write a Python program to print the unique values from a list of dictionaries.

Advertisement
Input
[{"name1": "robert"}, {"name2": "john"}, {"name3": "jim"}, {"name4": "robert"}]
Output
['robert', 'john', 'jim']

Approach

  1. Collect values in order and remove duplicates with dict.fromkeys.

Solution

Python
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)))
Output
['robert', 'john', 'jim']
Advertisement

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs