Python Dictionary items() Method

Python dictionary is a structure that is used to store data in key and value pairs. Each key and value pair is named as item . The dictionary type provides the items() method used to return a view of the dictionary items in a read and printable way. In this tutorial, we examine how to use the items() method in different ways. The view() method returns items as a list of tuples where key and value pairs are represented as tuples.

items() Method Syntax

The items() method has very simple syntax where there is no parameter or option.


Print Dictionary Items

The items() method can be used to print all items of the dictionary in a human-readable way. The dictionary items are returned as a list of tuples.

car = {"brand":"Renault","model":"Megane","year":2021}

dict_items([('brand', 'Renault'), ('model', 'Megane'), ('year', 2021)])

Convert Dictionary To List

As stated previously we can use the items() method in order to convert a dictionary into a list. After conversion, we can use the dictionary like a list to access the items.

car = {"brand":"Renault","model":"Megane","year":2021}

car_list = list(car.items())




Iterate Over Dictionary Items

We can also iterate over dictionary items by converting them into a list by using the items() method. In the following example, we convert the dictionary named car into the list named car_list. Then we iterate over every item by using the for loop.

car = {"brand":"Renault","model":"Megane","year":2021}

car_list = list(car.items())

for item in car_list:
('brand', 'Renault')
('model', 'Megane')
('year', 2021)

Leave a Comment