How to create a dictionary of sets in Python?


In this article, we will learn how to create a dictionary of sets in Python.

Methods Used

The following are the various methods used to accomplish this task −

  • Using the Naive Method

  • Using defaultdict() Method

  • Using setdefault() Method

Method 1: Using the Naive Method

In this method, we create a dictionary of sets by passing sets as values to the keys.

Syntax

{ ‘Key’: Set 1, ‘Key’:Set 2,…………..,’Key’: Set n}

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

  • Create a variable to store the dictionary containing the values as a set without duplicate elements (i.e, dictionary of sets).

  • Print the input dictionary of sets.

Example

The following program creates a dictionary of sets(without duplicates) in python using the Naive Method −

# creating a dictionary containing the values as set 
# without duplicate elements
# (dictionary of sets)
inputDict = {'Employ ID': {10, 11, 12, 14, 15},
      'Employ Age': {25, 30, 40, 35, 28}}

# printing the input dictionary of sets
print(inputDict)

Output

On execution, the above program will generate the following output −

{'Employ ID': {10, 11, 12, 14, 15}, 'Employ Age': {35, 40, 25, 28, 30}}

Creating a Set having Duplicates

In general, the set in python does not allow duplicates i,e which removes all the repeated elements.

Example

The below example shows that a python set will not allow duplicates.

The following program creates a dictionary of sets(containing duplicates) in python using the Naive Method −

# creating a dictionary containing the values as set
# with duplicates elements 
# the set does not allow duplicates 
inputDict = {'Employ ID': {10, 11, 12, 13, 13, 14, 15, 10, 12, 11},
		'Employ Age': {25, 30, 30, 40, 25, 35, 40, 28, 33, 25}}

# printing the input dictionary
print(inputDict)

Output

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

{'Employ ID': {10, 11, 12, 13, 14, 15}, 'Employ Age': {33, 35, 40, 25, 28, 30}}

In the above example, we can observe that all the duplicates are removed, and printed only the unique elements. Hence proved that a python set removes does not allow duplicates.

Method 2: Using defaultdict() Method

In this method, the default set will be created and the key-values pairs will be passed to it.

Syntax

defaultdict(set)

Passing the dictionary with key and value −

dictionary[“key”] |= {‘value1’, ‘value2′, ……………,’value n’}

Here,

  • dictionary − input dictionary

  • key − key of a dictionary

  • value − the value of a dictionary passed as set.

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

  • Use the import keyword to import the defaultdict from the collections module.

  • Use the defaultdict() method, to create an empty set of dictionary using the by-passing set as an argument to it.

  • Give the key-value pairs for the dictionary using the [] operator.

  • Print the created dictionary of sets.

Example

The following program creates a dictionary of sets in python using the defaultdict() function −

# importing defaultdict from the collections module
from collections import defaultdict

# creating an empty set of the dictionary using the
# defaultdict() method by passing set as argument to it
dictionary = defaultdict(set)

# giving the first key-value pair of a dictionary
dictionary["Employ ID"] |= {10, 11, 12, 14, 15}

# giving the second key-value pair of a dictionary
dictionary["Employ Age"] |= {25, 30, 40, 35, 28}

# printing the created dictionary of sets
print(dictionary)

Output

On execution, the above program will generate the following output −

defaultdict(<class 'set'>, {'Employ ID': {10, 11, 12, 14, 15}, 'Employ Age': {35, 40, 25, 28, 30}})

Method 3: Using setdefault() Method

The value of a key within the dictionary is returned by the setdefault() method. If not, a key and value are inserted into the dictionary.

Syntax

dict.setdefault(key, default_value)

here,

  • key − It is the key that must be searched in a dictionary.

  • default_value − It is the value of a specific key

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

  • Create a variable to store the input dictionary containing the value as a set without duplicate elements (i.e, dictionary of sets).

  • Use the setdefault() function, to access the values of 'Employ ID' as a set of the input dictionary by passing 'Employ ID' as an argument to it.

  • Use the setdefault() function, to access the values of 'Employ Age’ as a set of the input dictionary by passing 'Employ Age’ as an argument to it.

  • Create a new key-value pair with values as set using the setdefault() method.

  • Print the resultant dictionary after adding 3rd set.

Example

The following program creates a dictionary of sets and accesses its elements of it using the setdefault() method −

# creating a dictionary containing the values as sets
# (dictionary of sets)
inputDict = {'Employ ID': {10, 11, 12, 14, 15, 11},
             'Employ Age': {25, 30, 40, 35, 28, 28}}

# accessing the values of 'Employ ID' of the dictionary as a set
# using setdefault() function
print("Accessing Employ ID:", inputDict.setdefault('Employ ID'))

# accessing the values of 'Employ Age' of dictionary as a set
# using setdefault() function
print("Accessing Employ Age:", inputDict.setdefault('Employ Age'))

# set the third set of values for the dictionary using setdefault method
# Employee names
inputDict = inputDict.setdefault(
   'Employ Name', {'rohit', 'virat', 'pandya', 'smith', 'warner'})

# printing the dictionary after adding 3rd set
print(inputDict)

Output

On execution, the above program will generate the following output −

Accessing Employ ID: {10, 11, 12, 14, 15}
Accessing Employ Age: {35, 40, 25, 28, 30}
{'pandya', 'rohit', 'smith', 'virat', 'warner'}

Conclusion

In this article, we learned how to use 3 different methods to create a dictionary of sets in Python. This is necessary to remove duplicate data for certain keys, such as only keeping unique roll numbers, unique job ids, etc. We also learned how to use the setdefault() function to retrieve the dictionary of sets.

Updated on: 24-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements