Get Unique Values from List of Dictionaries in Python (4 Examples)

 

In this tutorial, you’ll learn how to get unique values from a list of dictionaries in Python.

Here you have an overview:

Let’s get right into the code!

 

Example List of Dictionaries

At the start, we will need to create a sample Python list of dictionaries to use in the following examples.

my_list = [{"name": "John", "age": 30}, 
 {"name": "Jane", "age": 25}, 
  {"name": "John", "age": 40}]
 
print(my_list)
# [{'name': 'John', 'age': 30}, 
# {'name': 'Jane', 'age': 25}, 
# {'name': 'John', 'age': 40}]

As you can see, my_list is a list of three dictionaries. How to get the unique values in it? Let’s take a look at some examples!

 

Example 1: Use Set Comprehension to Get Unique Values from List of Dictionaries

If you are looking for an easy way to retrieve the unique values from a list of dictionaries, you can do so by using set comprehension. Take a look at the following code.

unique_values = {x['name'] for x in my_list}
 
print(unique_values)
# {'Jane', 'John'}

In the code snippet above, we have created a set containing the unique values of the ‘name’ key from the dictionaries in my_list. First, x['name'] defines what values should be included in the set. Then, the iteration part for x in my_list specifies that we want to iterate over each dictionary x in my_list, with which the set is created. This set, called unique_values, is finally printed to the console.

 

Example 2: Use List Comprehension to Get Unique Values from List of Dictionaries

In this second example, we use a list comprehension with conditionals to retrieve the unique values from a list of dictionaries.

unique_values = []
 
[unique_values.append(x['name']) for x in my_list if x['name'] not in unique_values]
 
print(unique_values)
# ['John', 'Jane']

As you can see in the previous Python output, we have first initialized an empty list called unique_values that will store the unique values. Then, we have set the list comprehension, which iterates over each dictionary x in my_list and appends the value of ‘name’ to unique_values if it is not already present in the list. As a result, we have obtained the unique values ‘Jane’ and ‘John’ in my_list.

 

Example 3: Use for Loop to Get Unique Values from List of Dictionaries

Using a for loop can also be handy when looking for a way to retrieve the unique values from a list of dictionaries. Check it out!

unique_values = []
 
for x in my_list:
    if x['name'] not in unique_values:
        unique_values.append(x['name'])
 
print(unique_values)
# ['John', 'Jane']

We have initialized the empty list unique_values. Then, we used a for loop to iterate through my_list and an if statement to check if the value of the ‘name’ key in the current dictionary x is not already present in unique_values list. If so, it is appended to the new list unique_values list using the append() method.

 

Example 4: Use lambda & map() Functions to Get Unique Values from List of Dictionaries

This last method uses the lambda and map() functions to select the unique values from a list of dictionaries.

unique_values = set(map(lambda x: x['name'], my_list))
 
print(unique_values)
# {'Jane', 'John'}

Here, we have used the map() function to apply the lambda function that searches for the ‘name’ values in each dictionary in the list. Finally, we used the set() function to retrieve the unique values in the list.

 

Video, Further Resources & Summary

Do you need more explanations on how to retrieve the unique values from a list of dictionaries in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
 

The YouTube video will be added soon.

 

Furthermore, you could have a look at the related tutorials on Statistics Globe:

This post has shown how to select unique values from a list of dictionaries in Python. Please tell me about it in the comments if you have further questions.

 

Paula Villasante Soriano Statistician & R Programmer

This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get further information about her academic background and the other articles she has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top