Print all unique values in list of dictionaries in Python

avatar

Borislav Hadzhiev

Last updated: Sep 22, 2022

banner

Photo from Unsplash

Print all unique values in list of dictionaries in Python #

To print all unique values in a list of dictionaries:

  1. Use a for loop to iterate over the list.
  2. Use another for loop to iterate over the values of each dictionary.
  3. Add the values to a set object.
  4. Iterate over the set and print the unique values.
main.py
list_of_dicts = [ {'id': 'a'}, {'id': 'a'}, {'id': 'b'}, {'id_2': 'a'}, {'id_2': 'c'}, ] my_set = set() for dictionary in list_of_dicts: for value in dictionary.values(): my_set.add(value) print(my_set) # 👉️ {'a', 'c', 'b'} print(len(my_set)) # 👉️ 3 for element in my_set: # c # a # b print(element)

We declared a new variable that stores an empty set.

Set objects store an unordered collection of unique elements, so adding duplicate values to a set is not possible.

We used a for loop to iterate over the list of dictionaries.

The nested for loop is used to iterate over the values of each dictionary.

The dict.values method returns a new view of the dictionary's values.

main.py
my_dict = {'id': 1, 'name': 'bobbyhadz'} print(my_dict.values()) # 👉️ dict_values([1, 'bobbyhadz'])

On each iteration, we use the set.add() method to add the value to the set.

The set.add method adds the provided element to the set.

main.py
my_set = set() my_set.add('bobby') my_set.add('hadz') my_set.add('com') print(my_set) # 👉️ {'com', 'hadz', 'bobby'}

You can use a for loop to iterate over the set and print the unique values.

Use the len() function if you need to get the count of unique values in the list of dictionaries.

An alternative to using a set object is to use a list and only append unique values to the list.

main.py
list_of_dicts = [ {'id': 'a'}, {'id': 'a'}, {'id': 'b'}, {'id_2': 'a'}, {'id_2': 'c'}, ] unique_values = [] for dictionary in list_of_dicts: for value in dictionary.values(): if value not in unique_values: unique_values.append(value) print(unique_values) # 👉️ ['a', 'b', 'c'] print(len(unique_values)) # 👉️ 3 for element in unique_values: # a # b # c print(element)

On each iteration in the nested for loop, we use the not in operator to check if the value is not present in the list.

If the value is not present in the list, we use the list.append() method to add it.

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l, otherwise, it evaluates to False.

x not in s returns the negation of x in s.

The list.append() method adds an item to the end of the list.

This should be your preferred approach if you need to preserve the order of the values.

Alternatively, you can use a generator expression with a nested for loop.

main.py
list_of_dicts = [ {'id': 'a'}, {'id': 'a'}, {'id': 'b'}, {'id_2': 'a'}, {'id_2': 'c'}, ] unique_values = set( value for dictionary in list_of_dicts for value in dictionary.values() ) print(unique_values) # 👉️ {'b', 'a', 'c'} print(len(unique_values)) # 👉️ 3 for element in unique_values: # b # a # c print(element)

We used a generator expression to iterate over the list of dictionaries.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

We also use a nested for loop to iterate over the values of each dictionary just like we did in the previous examples.

The last step is to use the set() class to convert the generator object to a set of unique values.

The for loop prints the unique values of the dictionaries on separate lines.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.