Python Dictionary items()

The dict.items() returns a dictionary view object that provides a dynamic view of dictionary elements as a list of key-value pairs. This view object changes when the dictionary changes.

Syntax:

dict.items()

Parameters:

No parameters.

Return Value:

Returns a view object dict_items that displays a list of dictionary's key-value pairs.

The following example demonstrates the dict.items() method.

Example:
romanNums = {'I':1,'II':2,'III':3,'IV':4}
dictview = romanNums.items()
print(dictview)
Output
dict_items([('I', 1), ('II', 2), ('III', 3), ('IV', 4)])

The following example shows how the view object changes when the dictionary is updated.

Example:
romanNums = {'I':1,'II':2,'III':3,'IV':4}
dictview = romanNums.items()
print("Dictionary View Object: ", dictview)
romanNums['V'] = 5
print("Dictionary View Object after adding new element: ", dictview)
Output
Dictionary View Object: dict_items([('I', 1), ('II', 2), ('III', 3), ('IV', 4)])
Dictionary View Object after adding new element:  dict_items([('I', 1), ('II', 2), ('III', 3), ('IV', 4), ('V', 5)])
Want to check how much you know Python?