Python Dictionary items() Method

Returns a list containing the tuple for each key-value pair. This is what Python calls a "view object" which means that the object can only be viewed - it cannot be changed, nor can the tuple items be accessed.

Syntax

Python
dictionary.items()

Example

Python
fish = {
    'guppy': 2,
    'zebra' : 5,
    'betta': 10
}

items = fish.items();
print(f'fish items: {items}')

Output

fish items: dict_items([('guppy', 2), ('zebra', 5), ('betta', 10)])

Notes

While a view object cannot be changed, it can be used in iterator functions like a for loop.

Example

Python
fish = {
    'guppy': 2,
    'zebra' : 5,
    'betta': 10
}

items = fish.items();
# We can iterate over the tuples:
for item in items:
    print(f'I have {item[1]} {item[0]}')

Output

I have 2 guppy
I have 5 zebra
I have 10 betta