Python Dictionary items() Method Tutorial

In this section we will learn what the Dictionary items() method is and how to use it in Python.

What is Dictionary items() Method in Python?

The Python Dictionary items() method is used to get a view of the target dictionary.

This view is basically a list with each pair of key/value of the target Dictionary set in a tuple within this list.

Note that we say a “view” object. That means both this view and the target dictionary are pointing to the same key/value pairs. So if we used the dictionary to change an item, then the view object will see that change as well.

Python Dictionary items() Method Syntax:

dictionary.items()

Dictionary items() Method Parameter:

The method does not take an argument.

Dictionary items() Method Return Value

The return value of this method is a view object that contains each pair of key/value of the target Dictionary set in a tuple.

Example: using python dictionary items() method

dictionary = {

"name":"Jack",

"lastName":"Doe"

}

view =dictionary.items()

print(view)

dictionary["name"] = "John"

print(view)

Output:

dict_items([('name', 'Jack'), ('lastName', 'Doe')])

dict_items([('name', 'John'), ('lastName', 'Doe')])

Leave a Reply