Python Dictionary items() method
Last Updated :
31 Jul, 2024
As of Python 3.7, dictionaries are ordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, a Dictionary holds a key: value pair.
In Python Dictionary, items() are the list with all dictionary keys with values.
Syntax: dictionary.items()
Parameters: This method takes no parameters.
Returns: A view object that displays a list of a given dictionary’s (key, value) tuple pair.
Example #1:
Python3
# Python program to show working
# of items() method in Dictionary
# Dictionary with three items
Dictionary1 = { 'A': 'Geeks', 'B': 4, 'C': 'Geeks' }
print("Dictionary items:")
# Printing all the items of the Dictionary
print(Dictionary1.items())
Output:
Dictionary items:
dict_items([('A', 'Geeks'), ('B', 4), ('C', 'Geeks')])
Order of these items in the list may not always be same.
Example #2: To show working of items() after modification of Dictionary.
Python3
# Python program to show working
# of items() method in Dictionary
# Dictionary with three items
Dictionary1 = { 'A': 'Geeks', 'B': 4, 'C': 'Geeks' }
print("Original Dictionary items:")
items = Dictionary1.items()
# Printing all the items of the Dictionary
print(items)
# Delete an item from dictionary
del[Dictionary1['C']]
print('Updated Dictionary:')
print(items)
Output:
Original Dictionary items:
dict_items([('A', 'Geeks'), ('C', 'Geeks'), ('B', 4)])
Updated Dictionary:
dict_items([('A', 'Geeks'), ('B', 4)])
If the Dictionary is updated anytime, the changes are reflected in the view object automatically.
Please Login to comment...