In this article, we will discuss the Python Dictionary items() method. In addition to this, we will discuss the item() method syntax, usage, and examples.
Dictionary items() method in Python
Below are the topics that we are doing to discuss in this article:
- Introduction to Python Dictionary items() method
- Syntax of the items() method
- Examples of the items() method
Python Dictionary items() method
The items() method is a built-in Python function that works with dictionaries. It returns a view object that presents a list of the Python dictionary’s key-value pairs as tuples.
Here’s the syntax for the items() method:
dictionary.items()
items) method in Python Dictionary Examples
Let’s dive into some examples to better understand the items() method in action:
Example#1 Basic Usage
player = {
'name': 'Babe Ruth',
'team': 'New York Yankees',
'position': 'Outfielder',
'batting_average': .342
}
print(player.items())
The items() method does not take any parameters and returns a view object that contains the Python dictionary’s key-value pairs in a tuple for each pair.
Output:
Example#2 Dynamic object return
player = {
'name': 'Babe Ruth',
'team': 'New York Yankees',
'position': 'Outfielder',
'batting_average': .342
}
items_view = player.items()
print('Before change:', items_view)
# Update the dictionary
player['batting_average'] = .345
print('After change:', items_view)
The view object returned from the items() method is dynamic, meaning it changes as the Python dictionary changes. As we can see, the items_view
object reflected the change we made to the 'batting_average'
in the player Python dictionary.
Output:
Example#3 Iterate over keys & values
player = {
'name': 'Babe Ruth',
'team': 'New York Yankees',
'position': 'Outfielder',
'batting_average': .342
}
for key, value in player.items():
print(key, ":", value)
The items() method is often used when we need to iterate over the keys and values of a Python dictionary. For instance, we might want to print out the player’s details.
Output:
Example#4 Iterate over key-value using for loop
presidents = {
'George Washington': [1789, 1797],
'John Adams': [1797, 1801],
'Thomas Jefferson': [1801, 1809],
'James Madison': [1809, 1817]
}
for president, term in presidents.items():
print(f"{president} served as president from {term[0]} to {term[1]}.")
Here, we create a Python dictionary where each key is the name of a U.S. president and the corresponding value is a Python list representing the start and end years of their presidency.
Then uses a for-loop to iterate over this dictionary. During each iteration, it prints a formatted string that includes the president’s name (the key) and the years they served in office (the values).
The items() method is used to return the key-value pairs from the Python dictionary, which are then unpacked into the variables used in for loop.
Output:
Conclusion
Python dictionary items() method is a convenient tool for extracting and working with the key-value pairs of a dictionary. It enables dynamic views of the dictionary content, which reflects any changes to the dictionary.
This makes the method especially useful in scenarios where we need to iterate over the keys and values of a dictionary, making your code cleaner and more efficient.
You may also like to read the following articles:
- Python dictionary clear() method [With Examples]
- Python Dictionary of sets
- Python dictionary key error
- Python dictionary copy() method [With Examples]
- Python dictionary increment value
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.