items() method in Python Dictionary

It is a method in python that returns a view object which contains of all the item in the dictionary as key-value pair inside a tuple in a list.

Syntax:

dictionary.items()

Don’t know what is get method in Python? Read: get() 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 of all the items in key-value pair inside a tuple, in a list.

Example 2:

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

Output:

dict_items([(‘site’, ‘CodeWindow’), (‘country’, ‘India’), (‘type’, ‘News’), (‘platform’, ‘IT’)])

Explanation:

Here it returned a view object of all the items in key-value pair inside a tuple, in a list.


You Missed

Also Checkout