Open In App

Python | Convert a set into dictionary

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes we need to convert one data structure into another for various operations and problems in our day to day coding and web development. Like we may want to get a dictionary from the given set elements.
Let’s discuss a few methods to convert given set into a dictionary.

Method #1: Using fromkeys() 

Python3




# Python code to demonstrate
# converting set into dictionary
# using fromkeys()
 
# initializing set
ini_set = {1, 2, 3, 4, 5}
 
# printing initialized set
print ("initial string", ini_set)
print (type(ini_set))
 
# Converting set to dictionary
res = dict.fromkeys(ini_set, 0)
 
# printing final result and its type
print ("final list", res)
print (type(res))


Output: 

initial string {1, 2, 3, 4, 5}
<class 'set'>
final list {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}
<class 'dict'>

 

Method #2: Using dict comprehension 

Python3




# Python code to demonstrate
# converting set into dictionary
# using dict comprehension
 
 
# initializing set
ini_set = {1, 2, 3, 4, 5}
 
# printing initialized set
print ("initial string", ini_set)
print (type(ini_set))
 
str = 'fg'
# Converting set to dict
res = {element:'Geek'+str for element in ini_set}
 
# printing final result and its type
print ("final list", res)
print (type(res))


Output

initial string {1, 2, 3, 4, 5}
<class 'set'>
final list {1: 'Geekfg', 2: 'Geekfg', 3: 'Geekfg', 4: 'Geekfg', 5: 'Geekfg'}
<class 'dict'>

Using the zip() function:

Approach:

Use the zip() function to create a list of keys and a list of default values.
Use a dictionary comprehension to create a dictionary from the two lists.

Python3




def set_to_dict(s):
    keys = list(s)
    values = [None] * len(s)
    return {k: v for k, v in zip(keys, values)}
 
s = {1, 2, 3}
print(set_to_dict(s))


Output

{1: None, 2: None, 3: None}

Time complexity: O(n)
Space complexity: O(n)

 



Similar Reads

Python | Convert flattened dictionary into nested dictionary
Given a flattened dictionary, the task is to convert that dictionary into a nested dictionary where keys are needed to be split at '_' considering where nested dictionary will be started. Method #1: Using Naive Approach Step-by-step approach : Define a function named insert that takes two parameters, a dictionary (dct) and a list (lst). This functi
8 min read
Python | Convert nested dictionary into flattened dictionary
Given a nested dictionary, the task is to convert this dictionary into a flattened dictionary where the key is separated by '_' in case of the nested key to be started. Method #1: Using Naive Approach Step-by-step approach : The function checks if the input dd is a dictionary. If it is, then it iterates over each key-value pair in the dictionary, a
8 min read
Python program to convert Set into Tuple and Tuple into Set
Let's see how to convert the set into tuple and tuple into the set. For performing the task we are use some methods like tuple(), set(), type(). tuple(): tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.set(): set method is to convert other type values to set this meth
7 min read
Convert String Dictionary to Dictionary Python
Interconversions of data types have been discussed many times and have been quite a popular problem to solve. This article discusses yet another problem of interconversion of the dictionary, in string format to a dictionary. Let's discuss certain ways in which this can be done. Convert String Dictionary to Dictionary Using json.loads() This task ca
6 min read
Convert Dictionary Value list to Dictionary List Python
Sometimes, while working with Python Dictionaries, we can have a problem in which we need to convert dictionary list to nested records dictionary taking each index of dictionary list value and flattening it. This kind of problem can have application in many domains. Let's discuss certain ways in which this task can be performed. Input : test_list =
9 min read
Python | Convert a list of Tuples into Dictionary
Sometimes you might need to convert a tuple to dict object to make it more readable. In this article, we will try to learn how to convert a list of tuples into a dictionary. Here we will find two methods of doing this. Examples: Input : [("akash", 10), ("gaurav", 12), ("anand", 14), ("suraj", 20), ("akhil", 25), ("ashish", 30)] Output : {'akash': [
6 min read
Python | Convert two lists into a dictionary
Interconversion between data types is usually necessary for real-time applications as certain systems have certain modules that require input in a particular data type. Let's discuss a simple yet useful utility of conversion of two lists into a key: value pair dictionary in Python. Converting Two Lists into a DictionaryPython List is a sequence dat
9 min read
Python | Convert dictionary object into string
The dictionary is an important container and is used almost in every code of day-to-day programming as well as web development with Python. The more it is used, the more is the requirement to master it and hence it's necessary to learn about them. Input: { "testname" : "akshat","test2name" : "manjeet","test3name" : "nikhil"}Output: {"testname": "ak
3 min read
Python | Convert key-value pair comma separated string into dictionary
Given a string, with different key-value pairs separated with commas, the task is to convert that string into the dictionary. These types of problems are common in web development where we fetch arguments from queries or get a response in the form of strings. Given below are a few methods to solve the task. Method #1: Using dictionary comprehension
5 min read
Python | Convert list of tuple into dictionary
Given a list containing all the element and second list of tuple depicting the relation between indices, the task is to output a dictionary showing the relation of every element from the first list to every other element in the list. These type of problems are often encountered in Coding competition. Below are some ways to achieve the above task. I
8 min read