Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
python3.8
dictionary
communitycreator

How does items() work in Python?

Hassan Ahmed

The inbuilt items() method in Python is used to return a view object, which contains the key-value pairs of a dictionary. These key-value pairs will be returned as tuples 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

dictionary.items()

Parameter

This method does not require any parameters.

Example 1

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

print(x)

Example 2

#This example represents how the returned 
#view object is dynamic 

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

postcodes.update({'civic': 2601})

print(x)

RELATED TAGS

python
python3.8
dictionary
communitycreator
RELATED COURSES

View all Courses

Keep Exploring