Python Dictionary items() method with Examples

Python Dictionary items

Now, we are going to have a look at the items method. The items method returns a view object, which contains the key-value pairs of the dictionary, as tuples. Let’s have a look at an example program, which would help us to understand the items method, and also help us to visualize the output.

Python Dictionary items() method

As you can see in the above program, we have a simple dictionary, and then we try to use the items method. With the items method, we get a view object, which contains all the key, value pairs as tuples. We are directly printing the output, so Let’s have a look at the output now.

dict_items([(‘one’, 1), (‘two’, 2), (‘three’, 3)])

As you can see, the key-value pairs are there as tuples in a list. So, you can use the items method as and when required. Remember that the items method returns an object which contains the key-value pairs from the dictionary.