Python Tutorial Python Advanced Python References Python Libraries

Python Dictionary - keys() Method



The Python keys() method is used to display a list containing all keys present in the dictionary. If the dictionary is modified, the display object also gets updated.

Syntax

dictionary.keys()

Parameters

No parameter is required.

Return Value

Returns dict_keys containing all keys present in the dictionary.

Example:

In the example below, the keys() method is used to display all keys present in the given dictionary.

Info = {
  'name': 'John',
  'age': 25,
  'city': 'London'
}
#display a list of all keys present in the dictionary.
x =  Info.keys()   
print(x)

#Adding a new field, display object also gets changed. 
Info['hobby'] = 'Swimming'
print(x)

The output of the above code will be:

dict_keys(['name', 'age', 'city'])
dict_keys(['name', 'age', 'city', 'hobby'])

❮ Python Dictionary Methods