Set of keys in Python 3 dictionary

Question:

I would like to glean the set of keys from a dictionary in Python 3.9.

The documentation on dictionary views does not explicitly mention conversion to a set but, naively, I would expect this to work:

d = {'one': 1, 'two': 2, 'three': 3}
s_of_k = set(d.keys)

But it throws this error:

TypeError: ‘builtin_function_or_method’ object is not iterable

How might I instantiate the keys of a Python 3 dictionary as a set?

Asked By: dumbledad

||

Answers:

d = {'one': 1, 'two': 2, 'three': 3}
s_of_k = set(d.keys())
Answered By: Plutonergy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.