Python Practice › Set Programs › Program 42

Set to a dictionary

EasySet Programs · 42 of 48

Write a Python program to convert a set into a dictionary.

Advertisement
Input
{"a", "b", "c"}
Output
{'a': 0, 'b': 0, 'c': 0}
{'a': 97, 'b': 98, 'c': 99}

Approach

  1. dict.fromkeys(s, value) or a comprehension.

Solution

Python
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.

More set programs

← All 48 Set Programs

Leave a Comment