Python Dictionary items

Python dictionary items() function returns a view object of available dictionary items (total keys and value pairs) as individual tuples. When we modify the dictionary, the view object updates the modifications and returns the new view object. In this section, we discuss the use of this dictionary’s item function.

Python Dictionary items() Syntax

The syntax of the built-in dictionary items() function is

dictionary_name.items()

Parameters: It does not take any parameters.

Return Value: The items() function returns a view object containing a list of dictionary (key, value) pairs as a tuple. Basically, it returns a list of tuples, and each tuple contains a dictionary key-value pair.

Python Dictionary items() function Example

The items function returns the list of total key-value pairs available in a given dictionary. The code below prints the key-value pairs in emp and employ.

TIP: Please refer to the Dictionary and Dictionary methods articles. For more tutorials, visit Python page.

emp = {'name': 'Kevin', 'age': 25 , 'Sal': 725000}
print("Dictionary: ", emp)

# Print Items
print("Items: ", emp.items())

# Creating an Empty 
employ = {}
print("\nDictionary: ", employ)

# Print Values
print("Dictionary Items: ", employ.items())
Python Dictionary items function example

Using the dictionary items() function inside a loop

In the following example, the items() method is used inside a for loop to iterate over the dictionary items and access keys and values. On each iteration, unpack the tuple items to access and print the dictionary key-value pair returned by the items() method.

n = {'a': 10, 'b': 20, 'c': 30}
for k, v in n.items():
print(k, v)
a 10
b 20
c 30

If we use the code below, the for loop accesses the individual tuples without unpacking.

n = {'a': 10, 'b': 20, 'c': 30}
for i in n.items():
    print(i)
('a', 10)
('b', 20)
('c', 30)

How items() work when the dictionary is modified?

The dictionary items() function automatically updates the view object accordingly.

n = {'a': 10, 'b': 20, 'c': 30}

items = n.items()
print('Original:', items)

del[n['b']]
print('Updated:', items)
Original: dict_items([('a', 10), ('b', 20), ('c', 30)])
Updated: dict_items([('a', 10), ('c', 30)])

Using Python dictionary items() with conditional statements

Once the items() function extracts the key-value pair, we can use any conditional statement to filter those tuple pairs. Here, the if statement checks the GDP position and if it is greater than 2, print a message.

n = {'INDIA': 4, 'USA': 1, 'CHINA': 2, 'JAPAN': 3}

for countries, position in n.items():
    if position > 2:
        print(f"{countries} should improve its GDP.")
INDIA should improve its GDP.
JAPAN should improve its GDP.

Using items() with dictionary comprehension

The following example creates a new dictionary based on an existing one by filtering items based on a condition. For this, we use dictionary comprehension with the items() method to apply a condition.

n = {'Bikes': 10, 'Cars': 20, 'Cycle': 50}

n = {k: v for k, v in n.items() if v > 15}
print(n)
{'Cars': 20, 'Cycle': 50}