Python Dictionary keys() Method Tutorial

In this section we will learn what the Dictionary keys() method is and how to use it in Python.

What is Dictionary keys() Method in Python?

The Python Dictionary keys() method is used to get an object view of the target Dictionary that contains all its keys set as the items on a list.

Note that we’re saying a “view” of the keys in the target Dictionary! This means if one of the keys in the target dictionary changed, this view object will notice the change as well.

Python Dictionary keys() Method Syntax:

dictionary.keys()

Dictionary keys() Method Parameter:

The method does not take an argument.

Dictionary keys() Method Return Value

The return value of this method is view object that contains the entire keys of the target Dictionary set as the items in a list.

Example: using python dictionary keys() method

dictionary = {

"name":"Jack",

"lastName":"Doe"

}

result = dictionary.keys()

print(result)

dictionary["age"] = 200

print(result)

Output:

dict_keys(['name', 'lastName'])

dict_keys(['name', 'lastName', 'age'])
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies