Signup/Sign In

Python Program to Extract Unique values from a dictionary

In this tutorial, we will learn to extract unique values from a dictionary in Python. Dictionary in Python is a data type for an ordered collection of data values. Dictionaries hold a key-value pair where each value is linked with a unique key.

Look at the example to understand the input-output format.

Input: {

'A' : [1, 3, 5, 4],
'B' : [4, 6, 8, 10],
'C' : [6, 12, 4 ,8],
'D' : [5, 7, 2]

}

Output: [1, 2, 3, 4, 5, 6, 7, 8, 10, 12]

To solve this problem in Python, we can use the following approaches:

  1. Using sorted(), values() and list comprehension
  2. Using sorted(), values() and chain()

Let's look at all the approaches in detail.

Approach 1: sorted(), values() and list comprehension

In this approach, we will be using a combination of these methods to extract unique values from a dictionary. We will use the values() method to extract values from the dictionary. List comprehension will be used to get unique values from the list. sorted() method is simply used to sort the unique values in ascending order.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Initialise a dictionary with keys and values

Step 2- Print the original dictionary

Step 3- Declare a list to store the result

Step 4- Using values() to get the values from the dictionary

Step 5- Use list comprehension to get values that are not already present in the dictionary

Step 6- Use the sorted() method to sort the elements in ascending order

Step 7- Print the result

Python Program 1

Look at the program to understand the implementation of the approach. To get the elements in a particular order, we have used the sorted() method. By using for loop in list comprehension, we have stored only the unique values from the dictionary into the result list.

dict1 = {'A' : [1, 3, 5, 4],
             'B' : [4, 6, 8, 10],
             'C' : [6, 12, 4 ,8],
             'D' : [5, 7, 2]}

print("The original dictionary is : " ,dict1)
  
# Using list comprehension, values() and sorted()
res = list(sorted({ele for val in dict1.values() for ele in val}))
  
# print result 
print("The unique values list is : " , res) 


The original dictionary is : {'A': [1, 3, 5, 4], 'B': [4, 6, 8, 10], 'C': [6, 12, 4, 8], 'D': [5, 7, 2]}
The unique values list is : [1, 2, 3, 4, 5, 6, 7, 8, 10, 12]

Approach 2: sorted(), values() and chain()

This is a similar approach to the above-mentioned approach, where the only difference is that we will be using the chain() method instead of a for loop in the list comprehension.

The chain() is a method of the itertools package, it returns a single iterable from a series of iterables.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Import chain in the program

Step 2- Initialise a test dictionary with keys and values

Step 3- Print the original dictionary

Step 4- Declare a list to store the result

Step 4- Using values() select the values from the dictionary

Step 5- In list comprehension use chain() to get values that are not already present in the dictionary

Step 6- Use the sorted() method to sort the elements in ascending order

Step 7- Print the result

Python Program 2

Look at the program to understand the implementation of the approach. We have imported the chain() method from itertools package in our program. We will be passing the dictionary values in the chain() method after unzipping the dictionary.

# unique values from a dictionary
from itertools import chain

# initializing dictionary
dict1 = {'A' : [10, 11, 2],
             'B' : [4, 6, 8, 10],
             'C' : [9, 10, 23, 11, 2],
             'D' : [10, 3, 4]}
  
# print original dictionary
print("The original dictionary is : " ,dict1)
  
# Using list comprehension, chain() values() and sorted()
res = list(sorted(set(chain(*dict1.values()))))
  
# print result 
print("The unique values list is : " , res) 


The original dictionary is : {'A': [10, 11, 2], 'B': [4, 6, 8, 10], 'C': [9, 10, 23, 11, 2], 'D': [10, 3, 4]}
The unique values list is : [2, 3, 4, 6, 8, 9, 10, 11, 23]

Conclusion

In this tutorial, we have seen two different ways of extracting only the unique elements from a dictionary. First, we have used a combination of methods and concepts like values(), sorted(), and loops. In the second approach, we have used a combination of methods like values(), sorted(), and chain()