C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python Dictionary items() MethodPython item() method returns a new view of the dictionary. This view is collection of key value tuples. This method does not take any parameter and returns empty view if the dictionary is empty. The examples and syntax are given below. Signature
items() Parameters
No parameter ReturnIt returns a dictionary's view. Let's see some examples of items() method to understand it's functionality. Python Dictionary items() Method Example 1
This is a simple example which returns all the items present in the dictionary.
# Python dictionary items() Method
# Creating a dictionary
student = {'name':'rohan', 'course':'B.Tech', 'email':'rohan@abc.com'}
# Calling function
items = student.items()
# Displaying result
print(items)
Output: dict_items([('name', 'rohan'), ('course', 'B.Tech'), ('email', '[email protected]')])
Python Dictionary items() Method Example 2If the dictionary is already empty, this method does not raise any error. See the example below.
# Python dictionary items() Method
# Creating a dictionary
student = {} # dictionary is empty
# Calling function
items = student.items()
# Displaying result
print(items)
Output: dict_items([]) Python Dictionary items() Method Example 3Apart from items() method, we can also use other customized approaches to fetch dictionary elements. See an example below.
# Python dictionary items() Method
# Creating a dictionary
student = {'name':'rohan', 'course':'B.Tech', 'email':'rohan@abc.com'}
# Iterating using key and value
for st in student:
print("(",st, ":", student[st], end="), ")
# Calling function
items = student.items()
# Displaying result
print("\n", items)
Output: ( name : rohan), ( course : B.Tech), ( email : [email protected]), dict_items([('name', 'rohan'), ('course', 'B.Tech'), ('email', '[email protected]')])
Next TopicPython Dictionary
|