Python Program to Extract Unique dictionary values

In this Python example, we will explore some ways to extract unique dictionary values.

Dictionaries are a powerful data structure in Python, allowing us to store key-value pairs. Sometimes, we may need to extract unique values from a dictionary. In this post, we will explore multiple ways to extract unique values from a dictionary in Python, including cases where the values are lists.

Some of the topics which will be helpful for understanding the program implementation better are:


1. Using set() and a loop

One way to extract unique values from a dictionary is to use the set() function along with a loop to iterate over the values in the dictionary. This works well if the values are not lists, as sets only contain unique elements.

Here is an example:

my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 2}
unique_values = set()
for value in my_dict.values():
  unique_values.add(value)
print(unique_values)
Output
{1, 2, 3}

2. Using set() and a list comprehension

If we prefer a more concise syntax, we can use a list comprehension inside the set() function to extract the unique values in one line of code. Here is an example:

my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 2}
unique_values = set([value for value in my_dict.values()])
print(unique_values)
Output
{1, 2, 3}

3. Using list comprehension for dictionaries with list values

If the values in the dictionary are lists, we can use list comprehension to extract the unique values from all lists in the dictionary. Here is an example:

my_dict = {'a': [1, 2, 3], 'b': [2, 3, 4], 'c': [3, 4, 5]}
unique_values = set([value for values in my_dict.values() for value in values])
print(unique_values)
Output
{1, 2, 3, 4, 5}

4. Using set() and itertools.chain() for dictionaries with list values

We can also use itertools.chain() to flatten the lists in the dictionary and then use the set() function to extract the unique values. Here is an example:

import itertools
my_dict = {'a': [1, 2, 3], 'b': [2, 3, 4], 'c': [3, 4, 5]}
unique_values = set(itertools.chain(*my_dict.values()))
print(unique_values)
Output
{1, 2, 3, 4, 5}

5. Conclusion

In conclusion, extracting unique values from a dictionary is a common task in Python programming. Depending on the data structure of the values, we can use different techniques such as loops, list comprehension, and itertools to achieve this task efficiently.

This article exhibits multiple ways to extract unique values from a dictionary.


Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index