In this tutorial, we’ll learn how to use Python dictionary items() method step by step using proper Python code examples.
Outline
- Introduction: Python Dictionary items() Method
- Syntax of items() Method
- Example 1: Fetch items from Dictionary using items() Method
- Example 2: Updating Dictionary Effects of items() Method
- Example 3: items() Method Return Value
- Conclusion
Introduction: Python Dictionary items() Method
This method returns a view object displaying a list of tuples having specified dictionary’s item pairs(keys and values of dictionary).
Syntax of items() Method
dictionaryName.items()
- It does not take any arguments.
- This Python dictionary items() method returns a view object displaying a list of tuples having the item pairs(key and value) of specified dictionary.
Example 1: Fetch items from Dictionary using items() Method
items={'a':34,'b':'white','c':62.5} print(items.items())
Output
dict_items([('a', 34), ('b', 'white'), ('c', 62.5)])
All the items(key, value) of dictionary are now shown in a list of tuples.
Example 2: Updating Dictionary Effects of items() Method
items={'a':34,'b':'white','c':62.5} print(items.items()) del [items['b']] // delete key value pair which have key(b) print(items.items())
Output
dict_items([('a', 34), ('b', 'white'), ('c', 62.5)]) dict_items([('a', 34), ('c', 62.5)]) // updated
If the dictionary is updated then the changed can be seen in the view object as well.
Example 3: items() Method Return Value
items={'a':34,'b':'white','c':62.5} returnedValue=items.items() print(returnedValue)
Output
dict_items([('a', 34), ('b', 'white'), ('c', 62.5)])
As you can see that this Python dictionary items() method returns a view object displaying a list of tuples having the item pairs(key and value) of the given dictionary.
Conclusion
To conclude, hope you now have a detailed practical understanding of how to use Python dictionary items() method. I’d be looking forward to have your valuable feedback on this post. Thank you for reading it.
You may like to read:
How To Use Python Dict FromKeys Method – Easy Python Guide
How To Use Python Dictionary Clear Method – Easy Python Code Example