Python Dictionary keys() Method
Returns a list of the dictionary's keys. The returned object is a "view object" - it cannot be changed.
Syntax
Python
Copy Code
dictionary.keys()
Example
Python
Copy Code
fish = { 'guppy': 2, 'zebra' : 5, 'betta': 10 } keys = fish.keys(); print(f'My fish: {keys}')
Output
Copy Code
My fish: dict_keys(['guppy', 'zebra', 'betta'])
Notes
Often the dict_keys
, a "view object", is converted to a list.
Example
Python
Copy Code
fish = { 'guppy': 2, 'zebra' : 5, 'betta': 10 } keyList = list(fish.keys()) print(f'My fish: {keyList}')
Output
My fish: ['guppy', 'zebra', 'betta']