Python Dictionary - items() Method
The Python items() method is used to display all key-value pair present in the dictionary. It displays key-value in a list with each pair in a tuple. If the dictionary is modified, the display object also gets updated. See the below example for more details:
Syntax
dictionary.items()
Parameters
No parameter is required.
Return Value
Returns dict_items containing all key-value pair present in the dictionary.
Example:
In the example below, the items() method is used to display all key-value pairs in the given dictionary.
Info = { 'name': 'John', 'age': 25 } #display a list of tuple containing all key-value pairs. x = Info.items() print(x) #Adding a new field, display object also gets changed. Info['city'] = 'London' print(x)
The output of the above code will be:
dict_items([('name', 'John'), ('age', 25)]) dict_items([('name', 'John'), ('age', 25), ('city', 'London')])
❮ Python Dictionary Methods