AppDividend
Latest Code Tutorials

How to Convert Python Set to Dictionary

0

Python implements a lot of flexibility to handle different types of data structures. Converting from one data type to another is effortless. Sets are useful data type to save multiple elements in a single variable. Dictionaries are utilized to save data in key-value pairs.

Convert Python Set to Dictionary

To convert Python Set to Dictionary, use the fromkeys() method. The fromkeys() is an inbuilt function that creates a new dictionary from the given items with a value provided by the user. Dictionary has a key-value data structure. So if we pass the keys as Set values, then we need to pass values on our own.

Syntax

dictionary.fromkeys(keys, value)

Parameters

The keys parameter is required, and it is an iterable specifying the keys of the new dictionary.

The value parameter is optional and the value for all keys. The default value is None.

Implementation of converting set to dict

Define an initial set and print the set and its data type. Use the dict.fromkeys() method and pass the set and value 0 as an argument. Remember, to create a dictionary, and it takes keys and values. We provide the keys from set values, but we need to pass values on our own, and we selected 0 as each key’s value.

i_set = {'Vlad Tepes', 'Alucard', 'Trevor', 'Isaac', 'Carmilla'}
print(i_set)
print(type(i_set))

# converting set to dictionary
dictionary = dict.fromkeys(i_set, 0)

# printing final result and its type
print(dictionary)
print(type(dictionary))

Output

{'Alucard', 'Isaac', 'Trevor', 'Vlad Tepes', 'Carmilla'}
<class 'set'>
{'Alucard': 0, 'Isaac': 0, 'Trevor': 0, 'Vlad Tepes': 0, 'Carmilla': 0}
<class 'dict'>

The output dictionary has all the keys from Set’s elements, and the values are 0. The 0 is a default value we have passed in the dictionary comprehension. The comprehension works for lists as well; it is the most effective method to create a dictionary from the set or any sequence.

Please one thing to note that all values are references to that one default you passed into dict.fromkeys(), so be cautious when that default value is a mutable object. This is the one way to create a dictionary from Set in Python.

Using zip and dict to convert set to dict

Python dict() method can be used to take input parameters and convert them into a dictionary. We also need to use the zip() function to group the keys and values, which finally becomes the key-value pair of the dictionary.

set_keys = {'Vlad Tepes', 'Alucard', 'Trevor', 'Isaac'}
set_values = {1, 2, 3, 4}

# converting set to dictionary
dictionary = dict(zip(set_keys, set_values))

# printing final result and its type
print(dictionary)
print(type(dictionary))

Output

{'Vlad Tepes': 1, 'Isaac': 2, 'Trevor': 3, 'Alucard': 4}
<class 'dict'>

The zip() method returns an iterable, and using dict() method, we convert that iterable to the dictionary.

Python set to dict using dictionary comprehension

To convert set to dict, use the dictionary comprehension. Dictionary comprehension is a way to create dictionaries from particular expressions. 

The expression of dictionary comprehension is: {key: value for (key, value) in iterable}

i_set = {'Vlad Tepes', 'Alucard', 'Trevor', 'Isaac', 'Carmilla'}
print(i_set)
print(type(i_set))

# converting set to dictionary
dictionary = {element: 'Castlevania' for element in i_set}

# printing final result and its type
print(dictionary)
print(type(dictionary))

Output

{'Trevor', 'Vlad Tepes', 'Alucard', 'Carmilla', 'Isaac'}
<class 'set'>
{'Trevor': 'Castlevania', 'Vlad Tepes': 'Castlevania', 'Alucard': 'Castlevania', 
 'Carmilla': 'Castlevania', 'Isaac': 'Castlevania'}
<class 'dict'>

In this example, the value provided by the user is “Castlevania“. Hence the values for all the dictionary keys are Castlevania. That is it for converting set to dict in Python.

Wrapping up

You can use any of the three approaches like fromkeys(), a combination of zip() and dict(), or dictionary comprehension based on your requirements to convert a set to a dictionary or create a dictionary from set values.

See also

Python set to string

Python set to list

Python tuple to set

Python list to set

Python set comprehension

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.