Python 3 – dictionary items() Method
In Python, dictionaries are data structures that store key-value pairs. The items()
method is a built-in function in Python that returns a view of the dictionary’s key-value pairs as tuples. In this tutorial, we will explore the items()
method and how to use it in Python 3.
Syntax:
dict.items()
The items()
method returns a view object that contains all the items of the dictionary in the form of tuples. Each tuple contains two values – the key and the corresponding value.
Here’s an example usage of the items()
method:
# Create dictionary
my_dict = {"apple": 50, "banana": 20, "orange": 30}
# Use items() method to get key-value pairs
kv_pairs = my_dict.items()
# Print the output
print(kv_pairs)
Output:
dict_items([('apple', 50), ('banana', 20), ('orange', 30)])
Here, we have created a dictionary my_dict
with three key-value pairs. Then, we have used the items()
method to get all the key-value pairs from the dictionary. The output that we have obtained is a view object that contains all the items of the dictionary in the form of tuples.
Example: Using items() method for iterating through a dictionary
One use case of the items()
method is to iterate through the key-value pairs of a dictionary. Here is an example:
# Create dictionary
my_dict = {"apple": 50, "banana": 20, "orange": 30}
# Iterate through dictionary using items() method
for key, value in my_dict.items():
print(key, value)
Output:
apple 50
banana 20
orange 30
In this example, we have created a dictionary my_dict
with three key-value pairs. We have then used the items()
method to iterate through the key-value pairs of the dictionary. Inside the for
loop, we have unpacked the key-value pair into variables key
and value
, and then printed them.
Example: Using items() method for comparing two dictionaries
Another use case of the items()
method is to compare two dictionaries. Here’s an example:
# Create two dictionaries
dict1 = {"apple": 50, "banana": 20, "orange": 30}
dict2 = {"apple": 40, "banana": 25, "orange": 30}
# Compare two dictionaries using items() method
for key, value in dict1.items():
if dict2[key] != value:
print(f"{key} is different in both dictionaries")
Output:
apple is different in both dictionaries
banana is different in both dictionaries
In this example, we have created two dictionaries dict1
and dict2
with three key-value pairs each. We have then used the items()
method to iterate through the key-value pairs of the first dictionary dict1
. Inside the for
loop, we have compared the corresponding values in the second dictionary dict2
with the values in the first dictionary. If any value is different, we have printed the key.
Example: Using items() method for dictionary comprehension
The items()
method can also be used to create a new dictionary using the key-value pairs from an existing dictionary. Here’s an example:
# Create dictionary
my_dict = {"apple": 50, "banana": 20, "orange": 30}
# Create new dictionary using items() method
new_dict = {key.upper(): value for key, value in my_dict.items()}
# Print the output
print(new_dict)
Output:
{'APPLE': 50, 'BANANA': 20, 'ORANGE': 30}
In this example, we have created a dictionary my_dict
with three key-value pairs. We have then used the items()
method to create a new dictionary new_dict
using the key-value pairs from the original dictionary. Inside the dictionary comprehension, we have used the upper()
method to convert the keys to uppercase.
Conclusion
In conclusion, the items()
method in Python 3 is a useful tool for working with dictionaries. It returns a view object that contains all the key-value pairs of a dictionary as tuples, which can be used for various purposes such as iterating through the dictionary, comparing two dictionaries or creating a new dictionary. The items()
method is a powerful function that can make your code more efficient and concise, and it is definitely worth learning if you are working with dictionaries in Python 3.