The syntax of items() method is:
dictionary.items()
The items() method is similar to dictionary's viewitems()
method in Python 2.7
The items() method doesn't take any parameters.
The items() method returns a view object that displays a list of a given dictionary's (key, value) tuple pair.
# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 } print(sales.items())
When you run the program, the output will be:
dict_items([('apple', 2), ('orange', 3), ('grapes', 4)])
# 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)
When you run the program, the output will be:
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 to the view object itself, as shown in the above program.
It takes a lot of effort and cost to maintain Programiz. We would be grateful if you support us by either:
Disabling AdBlock on Programiz. We do not use intrusive ads.
or
Donate on Paypal