Python Program to Set from dictionary values


In Python, dictionary is a implementation of a data structure known as an associative array. A dictionary is made up of a group of key-value pairs. Each key-value combination corresponds to a key and its corresponding value.

In this article, we will learn how to convert into a Set of dictionary values in python.

Methods Used

The following are the various methods to accomplish this task:

  • Using generator expression and {}

  • Using values() and set() functions

  • Using Counter() function

Example

Assume we have taken an input dictionary. We will now extract all the values of a dictionary and then convert those values into a set.

Input

inputDict = {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}

Output

{10, 5, 15}

In the above example, the values of the input dictionary(5, 10, 15, 5) are extracted and converted into a set. All the duplicates are removed while converting into a set.

Hence the output is {10, 5, 15} after removing all repeating values.

Using generator expression and {}

Here, we use a generator expression to get all the values, and the {} operator handles the removal of duplicate items and conversion to a set.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store the input dictionary.

  • Print the input dictionary.

  • Traverse through the input dictionary and convert the values of the input dictionary to set using the generator expression and {}.

  • Print the resultant set after converting values of an input dictionary to set.

Example

The following program returns a set after converting values of the input dictionary to set using generator expression and {} –

# input dictionary
inputDict = {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}

# printing input dictionary
print("Input dictionary:\n", inputDict)

# {} operator is used to convert to set
# converting the values of the input dictionary to set 
resultantSet = {inputDict[i] for i in inputDict}

# printing resultant set after converting values of input dictionary to set
print("Resultant set with values of input dictionary:", resultantSet)

Output

On executing, the above program will generate the following output –

Input dictionary:
 {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}
Resultant set with values of input dictionary: {10, 5, 15}

Using values() and set() functions

In this method we are going ot use combination of values() and set() function of python to covert a set from dictionary value. Here the set() function creates a set object. A set list will appear in random order because the items are not ordered. It removes all the duplicates.The dict.values() function provides a view object that displays a list of all the values in the dictionary in order of insertion.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Use the values() function to get all the values of the input dictionary and convert them into a set with the set() function. The set() function also removes all duplicates.

  • Print the resultant set after converting values of an input dictionary to set.

Example

The following program returns a set after converting values of the input dictionary to set using values() and set() functions –

# input dictionary
inputDict = {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}

# printing input dictionary
print("Input dictionary:\n", inputDict)

# getting the values of the input dictionary and converting them into a set 
# set() function also removes all duplicates 
resultantSet = set(inputDict.values())

# printing resultant set after converting values of input dictionary to set
print("Resultant set with values of input dictionary:", resultantSet)

Output

On executing, the above program will generate the following output –

Input dictionary:
 {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}
Resultant set with values of input dictionary: {10, 5, 15}

Using the Counter() function

In this method we are going to use counter() function in python to convert the set from dictionary values. Here, Counter() function is a sub-class that counts the hashable objects. It implicitly creates a hash table of an iterable when called/invoked.the Counter() function is used to get the frequency of values of the input dictionary.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Use the import keyword to import the Counter function from the collections module.

  • Create a variable to store the input dictionary.

  • Print the input dictionary.

  • Use the values() function to get values of the input dictionary and get the frequency of those values as key-value pairs using the Counter() function.

  • Use the keys() function to get the keys from the frequency of the above values and convert them into a list using the list()function(converts the sequence/iterable to a list).

  • Use the set() function to convert the result values list into a set.

  • Print the resultant set after converting values of an input dictionary to set.

Example

The following program returns a set after converting values of the input dictionary to set using the Counter() function –

# importing Counter from collections module 
from collections import Counter

# input dictionary
inputDict = {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}

# printing input dictionary
print("Input dictionary:\n", inputDict)

# getting the frequency of values of input dictionary as key-value pairs 
valuesFrequency = Counter(inputDict.values())

# getting the keys from the frequency of the above values and converting them into a list 
resultantList = list(valuesFrequency.keys())

# converting the result values list into a set 
resultantSet = set(resultantList)

# printing resultant set after converting values of input dictionary to set
print("Resultant set with values of input dictionary:", resultantSet)

Output

On executing, the above program will generate the following output –

Input dictionary:
 {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}
Resultant set with values of input dictionary: {10, 5, 15}

Conclusion

This article taught us three distinct ways to build a Set of dictionary values. We learned how to convert data to sets using built-in methods like the {} operator and set() function. We also learned how to utilize the Counter() function, which in this case performs the same function as set(), to get all the unique values from the iterable.

Updated on: 18-Aug-2023

80 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements