AppDividend
Latest Code Tutorials

Python Dictionary items() Function Example

0

Python dictionary items() is an inbuilt function that is used to get all the keys and associated values with those keys. Python dictionary is an unordered collection of data values used to store the data values like a map, which unlike other Data Types that hold only a single value as an element, Dictionary contains the key: value pair.

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()

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.

Syntax

dictionary_name.items()

The items() method does not take any argument as a parameter, and it just fetches the key and value using 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:

  1. No duplicate key is permitted.
  2. The dictionary values can be of any type, but the keys must be immutable types like numbers, tuples, or strings.
  3. Dictionary keys are case sensitive: Same key name but with the different situations are treated as different keys in Python dictionaries.

So, dictionary items() returns all the keys and associated values with those keys.

See also

Python dictionary to dataframe

Python dictionary to csv file

Python dictionary to string

Python dictionary to json

Python dictionary get()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.