Join Regular Classroom : Visit ClassroomTech

keys() method in Python Dictionary

It is a method in python that returns a view object which contains of all the keys in the dictionary in a list.

Syntax:

dictionary.items()

Don’t know what is items method in Python? Read: items() method in Python Dictionary

Example 1:

s = {0:"CodeWindow", 1:"India", 2:"News"}
x = s.items()
print(x)

Output:

dict_items([(0, ‘CodeWindow’), (1, ‘India’), (2, ‘News’)])

Explanation:

Here it returned a view object all the keys of the dictionary in a list.

Example 2:

s = {"site":"CodeWindow", "country":"India", "type":"News", "platform":"IT"}
x = s.keys()
print(x)

Output:

dict_keys([‘site’, ‘country’, ‘type’, ‘platform’])

Explanation:

Here it returned a view object all the keys of the dictionary in a list.


You Missed

Also Checkout