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
dictionary.keys()

Example

Python
fish = {
'guppy': 2,
'zebra' : 5,
'betta': 10
}

keys = fish.keys();
print(f'My fish: {keys}')

Output

My fish: dict_keys(['guppy', 'zebra', 'betta'])

Notes

Often the dict_keys, a "view object", is converted to a list.

Example

Python
fish = {
    'guppy': 2,
    'zebra' : 5,
    'betta': 10
}

keyList = list(fish.keys())
print(f'My fish: {keyList}')

Output

My fish: ['guppy', 'zebra', 'betta']