How the keys() method works in Python

The inbuilt keys() method in Python is used to return a view object that contains all the keys of a dictionary in a list.

A view object gives a dynamic view of the dictionary. Whenever the dictionary is altered, the view object will mirror those alterations.

Syntax

dict.keys()

Parameters

This method does not require any parameters.

Code

Example 1

postcodes = {'griffith':2603, 'belconnen':2617, 'phillip':2606}
x = postcodes.keys()
print(x)

Example 2

#This example represents how the returned
#view object is dynamic
postcodes = {'griffith':2603, 'belconnen':2617, 'phillip':2606}
x = postcodes.keys()
postcodes.update({'civic': 2601})
print(x)