Python Function Reference

dict.keys

Access the keys of the dictionary.

Python 3.13
dict.keys()

Return value

A dict_keys view that reflects changes to the dictionary; wrap it in list() to get a list.

Examples

List of dictionary keys

Python 3.13
a = {'x': 1, 'y': 2}
print(list(a.keys()))
['x', 'y']

The view shows new keys

Python 3.13
a = {'x': 1}
keys = a.keys()
a['y'] = 2
print(keys)
dict_keys(['x', 'y'])

Details

Iterating with for key in d and checking key in d work with keys without calling keys().

See also