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?
Answers:
d = {'one': 1, 'two': 2, 'three': 3}
s_of_k = set(d.keys())
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?
d = {'one': 1, 'two': 2, 'three': 3}
s_of_k = set(d.keys())