Post

Python Dictionary keys()

In python dictionary, the key() method will return a view object as a list that contains the keys of the specified dictionary.

Python Dictionary keys()

The syntax of keys() is:

1
2
dictionary.keys()

keys() Parameters

The keys() method does not take any parameters as arguments.

Let see an example of the keys() method.

Example 1: How to use the keys() method in a python dictionary?

1
2
cars = {"BMW" : 1,"TOYOTA" : 2,"TATA" : 3}
print(cars.keys())

Output:

1
2
dict_keys(['BMW', 'TOYOTA', 'TATA'])

The keys() method will return an empty list when an empty dictionary is passed and when any changes are done in the dictionary, the view object will also reflect these changes when passed.

Rules of keys()

There are no such rules for keys() method.

This post is licensed under CC BY 4.0 by the author.