45. Problem to convert set to dictionary with the help of Python

In this Python set program, we will convert set to dictionary with the help of the below-given steps.

What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.

Convert set to dictionary:

Steps to solve the program

1. Create a set using {}.
2. Add some elements in the set.
3. Create an empty dictionary.
4. Use a for loop to iterate over elements in the set.
5. During iteration add the element as key and an empty set ({}) as its value to the dictionary.
6. Print the dictionary.

				
					a = {1,2,4,6}
print("Original set: ",Set)
Dict = {}
for ele in a:
    Dict[ele] = {}
print("Dictionary: ",Dict)
				
			

Output :

				
					Original set:  {1, 2, 4, 6}
Dictionary:  {1: {}, 2: {}, 4: {}, 6: {}}
				
			

Related Articles

find the index of an element in a set.

create a set of even numbers from 1 to 20.

Leave a Comment