Dictionaries are another example of a Python data structure. The Dictionary is used to map or associate things you want to store the keys you need to get them. Python dictionary is just like a dictionary in the real world. It is defined into two elements: Keys and Values.
Python dictionary items() method returns a view object. The view object contains the key-value pairs of the Dictionary, as tuples in a list. The view object will reflect any changes done to the Dictionary.
Python Dictionary items()
The dictionary items() is a built-in Python function used to get all the keys and associated values with those keys. The dict.items() method does not take any argument.
The Dictionary is an unordered collection of Python data values used to store the data values like a map. Unlike other Data Types that hold only a single value as an element, Dictionary contains the key: value pair.
Syntax
dictionary_name.items()
Arguments
The items() method does not take any argument, and it just fetches the key and value using the items() method. Here, dictionary_name is the name of the Dictionary.
Return value
The items() method returns a view object of a list of the given Dictionary’s (key and value) tuple pair.
Programming Example
# app.py # Program to show the working of dictionary items() # Declaring a dictionary student = {'Name': 'Debasis', 'Roll': 21, 'Section': 'CSE132'} # Printing all items of the dictionary print(student) # now we will print all items using items() print("Items using dict items() :") item = student.items() print(item) # Now lets modify the dictionary student['Roll'] = 20 # Collecting items of the dictionary item = student.items() # Printing the dictionary items print("After update dictionary items are: ") print(item)
Output
{'Name': 'Debasis', 'Roll': 21, 'Section': 'CSE132'} Items using dict items() : dict_items([('Name', 'Debasis'), ('Roll', 21), ('Section', 'CSE132')]) After update dictionary items are: dict_items([('Name', 'Debasis'), ('Roll', 20), ('Section', 'CSE132')])
From the above example, we can see that we have declared a dictionary with a student’s details. After that, we have printed its keys and values.
Then we have fetched items of the Dictionary using the items() method. When we are printing the item, we can see that the Dictionary is being printed as a list, and its keys and values are stored in a tuple.
Let’s return the Dictionary’s key-value pairs.
See the following code.
# app.py CES = { "company": "Sony", "industry": "Automotive", "model": "Vision S", "year": 2020 } data = CES.items() print(data)
Output
➜ pyt python3 app.py dict_items([('company', 'Sony'), ('industry', 'Automotive'), ('model', 'Vision S'), ('year', 2020)])
Conclusion
Following are two crucial points while using dictionary keys:
- No duplicate key is permitted.
- The dictionary values can be of any type, but the keys must be immutable types like numbers, tuples, or strings.
- Dictionary keys are case sensitive: Same key name but with the different situations are treated as different keys in Python dictionaries.
So, dictionary items() return all the keys and associated values with those keys.