Python Dictionary items()

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

Example

marks = {'Physics':67, 'Maths':87}

print(marks.items())
# Output: dict_items([('Physics', 67), ('Maths', 87)])

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()

# random sales dictionary
sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }

print(sales.items())

Output

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

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

# random sales dictionary
sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }

items = sales.items()
print('Original items:', items) # delete an item from dictionary
del[sales['apple']]
print('Updated items:', items)

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.


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges