Get a list of unique dictionaries in Python

avatar

Borislav Hadzhiev

Last updated: Sep 22, 2022

banner

Photo from Unsplash

Get a list of unique dictionaries in Python #

To get a list of unique dictionaries:

  1. Use a dict comprehension to iterate over the list.
  2. Use the value of each id property as a key and the dictionary as a value.
  3. Use the dict.values() method to only get the unique dictionaries.
  4. Use the list() class to convert the result to a list.
main.py
list_of_dictionaries = [ {'id': 1, 'site': 'bobbyhadz.com'}, {'id': 2, 'site': 'google.com'}, {'id': 1, 'site': 'bobbyhadz.com'}, ] result = list( { dictionary['id']: dictionary for dictionary in list_of_dictionaries }.values() ) # 👇️ [{'id': 1, 'site': 'bobbyhadz.com'}, {'id': 2, 'site': 'google.com'}] print(result)

We used a dict comprehension to iterate over the list of dictionaries.

Dict comprehensions are very similar to list comprehensions.

They perform some operation for every key-value pair in the dictionary or select a subset of key-value pairs that meet a condition.

On each iteration, we set the value of the current id as the key and the actual dictionary as the value.

The keys in a dictionary are unique, so any duplicate values get filtered out.

We then use the dict.values() method to only return the unique dictionaries.

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'])

The last step is to use the list() class to convert the view object to a list containing the unique dictionaries.

The list class takes an iterable and returns a list object.

Here is the complete code snippet.

main.py
list_of_dictionaries = [ {'id': 1, 'site': 'bobbyhadz.com'}, {'id': 2, 'site': 'google.com'}, {'id': 1, 'site': 'bobbyhadz.com'}, ] result = list( { dictionary['id']: dictionary for dictionary in list_of_dictionaries }.values() ) # 👇️ [{'id': 1, 'site': 'bobbyhadz.com'}, {'id': 2, 'site': 'google.com'}] print(result)

Get a list of unique dictionaries using a for loop #

To get a list of unique dictionaries:

  1. Declare a new variable that stores an empty list.
  2. Use a for loop to iterate over the list of dictionaries.
  3. Use the list.append() method to add unique dictionaries to the new list.
main.py
list_of_dictionaries = [ {'id': 1, 'site': 'bobbyhadz.com'}, {'id': 2, 'site': 'google.com'}, {'id': 1, 'site': 'bobbyhadz.com'}, ] new_list = [] for dictionary in list_of_dictionaries: if dictionary not in new_list: new_list.append(dictionary) # 👇️ [{'id': 1, 'site': 'bobbyhadz.com'}, {'id': 2, 'site': 'google.com'}] print(new_list)

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

On each iteration, we use the not in operator to check if the dictionary is not present in the new list.

If the condition is met, we use the list.append() method to append the dictionary to the list.

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 l returns the negation of x in l.

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

main.py
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # 👉️ ['bobby', 'hadz', 'com']
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.