Python Dictionary items()

The items() method returns a view object that displays a list of dictionary's (key, value) tuple pairs.

Example


Syntax of Dictionary items()

The syntax of items() method is:

dictionary.items()

Note: items() method is similar to dictionary's viewitems() method in Python 2.7.


items() Parameters

The items() method doesn't take any parameters.


Return value from items()

The items() method returns a view object that displays a list of a given dictionary's (key, value) tuple pair.


Example 1: Get all items of a dictionary with items()

Output

dict_items([('apple', 2), ('orange', 3), ('grapes', 4)])

Example 2: How items() works when a dictionary is modified?

Output

Original items: dict_items([('apple', 2), ('orange', 3), ('grapes', 4)])
Updated items: dict_items([('orange', 3), ('grapes', 4)])

The view object items doesn't itself return a list of sales items but it returns a view of sales's (key, value) pair.

If the list is updated at any time, the changes are reflected on the view object itself, as shown in the above program.