Python Dictionary items()

Python Dictionary Items() Method

This is a detailed tutorial of the Python Dictionary items() method. Learn to fetch the list of keys, values as tuple pairs in a given dictionary.

Python Dictionary items()

The method dict.items() returns a collection object containing the list of tuples such that each tuple contains the dictionary items key & value pairs. Let’s say a dictionary contains 5 different key-value pairs. Then this method applied to this dictionary will return dict_list collection object that will contain five tuples. Each of these tuples will contain exactly two items, the first item will the key and the second will be the value associated with that particular key.

Syntax

In the following syntax, the variable dictItemPairs will now contain the collection of tuple pairs of the keys and value contained in the dict.

items() Parameters

As no additional information is required in the process of converting dictionary items into tuple pairs, so this method do not take any arguments.

items() Return Value

The method items() returns a view object called dict_items. This view object contains the collection of tuples such that each tuple defines each of the dictionary item’s key-value pair. The first index of each tuple contains the key and the second index of each tuple contains the value associated with that particular key.

Examples

Given below are two examples illustrating the usage of the dict.items() method.

Example 1. Basic Usage of the items() Method

The following code demonstrates the simple usage of the items() method.


#A Dictionary containing 4 items (key-value) pairs
bio = {
"name" : "Gurmeet Singh",
"gender" : "MALE",
"age" : 21,
"website" : "wtmatter.com"
}


#Using the items() method
items = bio.items()


print(items)


Output. Running the above program gives the following output to the console.

Example 2. Modifying the dictionary after using the method items()

Here we’re adding one more dictionary item after applying the items() method on the dictionary.


#A Dictionary containing 4 items (key-value) pairs
bio = {
"name" : "Gurmeet Singh",
"gender" : "MALE",
"age" : 21,
"website" : "wtmatter.com"
}


#Using the items() method
items = bio.items()


print("Before Modification", items)


#Modifying the original dictionary after using the items() method


bio["country"] = "India"


print("After Modification", items)


The output shows that if the dictionary items are changed even after applying this method, the same change will be reflected in the dictionary view object as well.

Output.

Example 3. Looping through Dictionary View Object

You can easily loop through the dict_items object using the Python For Loop.


#A Dictionary containing 4 items (key-value) pairs
bio = {
"name" : "Gurmeet Singh",
"gender" : "MALE",
"age" : 21,
"website" : "wtmatter.com"
}


#Using the items() method
items = bio.items()


for item in items: print("Key:",item[0],", Value:",item[1])


Each iteration will return a tuple object into the variable item. As we know the first index and the second index of the tuple contain the key and the value respectively, so we’ve used the 0 and 1 indexing to fetch the corresponding values.

Output.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *