The view object is returned by the items() method. The key-value pairs of the dictionary are stored in the view object as tuples in a list.
Any changes made to the dictionary will be reflected in the view object.
Syntax:
dictionary.items()
Parameters: This method has no parameters.
Return value: A list containing the key-value as tuple pairs of the dictionary is returned.
- Python Dictionary: Values() Function
- How Do You Perform a Python Division Operation?
- How to Perform the Python Division Operation?
Example1: Returns a list of Key-Value pairs
# Give the dictionary as static input and store it in a variable. gvn_dictnry = {'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400} # Apply items() function to the given dictionary and store it in another variable. rslt = gvn_dictnry.items() # Print the above result. print("The result after applying items() function to the given dictionary:\n", rslt)
Output:
The result after applying items() function to the given dictionary: dict_items([('hello', 100), ('this', 200), ('is', 300), ('btechgeeks', 400)])
Here, a list of all the key-value pairs in the given input dictionary is returned.
Example2: Returns String items
# Give the dictionary as static input and store it in a variable. gvn_dictnry = {'hello': 'p', 'this': 'q', 'is': 'r', 'btechgeeks': 's'} # Apply items() function to the given dictionary and store it in another variable. rslt = gvn_dictnry.items() # Print the above result. print("The result after applying items() function to the given dictionary:\n", rslt)
Output:
The result after applying items() function to the given dictionary: dict_items([('hello', 'p'), ('this', 'q'), ('is', 'r'), ('btechgeeks', 's')])
As a result, the dict.items() method is unaffected by the type of key or value and provides a list of all the items in the dict as tuple pairs.
Return an Empty Dictionary using items() Function
For an empty dictionary, the dict.items() method throws no error or exception. As a result, if a dictionary is empty, the dict.items() method returns an empty list.
Example
# Give the empty dictionary as static input and store it in a variable. gvn_dictnry = {} # Apply items() function to the given dictionary and store it in another variable. rslt = gvn_dictnry.items() # Print the above result. print("The result after applying items() function to the given dictionary:\n", rslt)
Output:
The result after applying items() function to the given dictionary: dict_items([])
Updating/Modifying a Dictionary
# Give the dictionary as static input and store it in a variable. gvn_dictnry = {'hello': 'p', 'this': 'q', 'is': 'r', 'btechgeeks': 's'} # Apply items() function to the given dictionary and store it in another variable. rslt = gvn_dictnry.items() # Print the above result. print("The result after applying items() function to the given dictionary:\n", rslt) print() # Modify the given dictionary. gvn_dictnry['hello'] = 'goodmorning' # Print the given Dictionary after modification using the items() function print("The given Dictionary after modification:\n", gvn_dictnry.items())
Output:
The result after applying items() function to the given dictionary: dict_items([('hello', 'p'), ('this', 'q'), ('is', 'r'), ('btechgeeks', 's')]) The given Dictionary after modification: dict_items([('hello', 'goodmorning'), ('this', 'q'), ('is', 'r'), ('btechgeeks', 's')])
Print a Dictionary without using items() Function
Example1
# Give the empty dictionary as static input and store it in a variable. gvn_dictnry = {} # Print the given dictionary print("The given dictionary is:\n", gvn_dictnry)
Output:
The given dictionary is: {}
We can display the dictionary’s elements directly in key-value form by calling the input dictionary object.
As a result, the preceding line of code would return an empty parenthesis”{}”.
Example2
# Give the dictionary as static input and store it in a variable. gvn_dictnry = {'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400} # Print the given dictionary print("The given dictionary is:\n", gvn_dictnry)
Output:
The given dictionary is: {'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}
Example3
# Give the dictionary as static input and store it in a variable. gvn_dictnry = {'hello': 'p', 'this': 'q', 'is': 'r', 'btechgeeks': 's'} # Print the given dictionary print("The given dictionary is:\n", gvn_dictnry) print() # Apply items() function to the given dictionary and store it in another variable. rslt = gvn_dictnry.items() # Print the above result. print("The result after applying items() function to the given dictionary:\n", rslt)
Output:
The given dictionary is: {'hello': 'p', 'this': 'q', 'is': 'r', 'btechgeeks': 's'} The result after applying items() function to the given dictionary: dict_items([('hello', 'p'), ('this', 'q'), ('is', 'r'), ('btechgeeks', 's')])
In Brief:
- The Python dict.items() method is used to show the dictionary data elements as a list of tuple pairs.
- The dict.items() method returns an empty list if a dictionary is empty.
- When we modify a dictionary, the dict.items() method incorporates the changes into the output list.