Python Practice › Set Programs › Program 42
Set to a dictionary
Write a Python program to convert a set into a dictionary.
Input
{"a", "b", "c"}Output
{'a': 0, 'b': 0, 'c': 0}
{'a': 97, 'b': 98, 'c': 99}Approach
dict.fromkeys(s, value)or a comprehension.
Solution
s = {"a", "b", "c"}print(dict.fromkeys(sorted(s), 0))print({x: ord(x) for x in sorted(s)})Output
{'a': 0, 'b': 0, 'c': 0}
{'a': 97, 'b': 98, 'c': 99}Learn the concept
New to this topic? Read the lesson, then come back.