Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconHow to Iterate through Dictionary in Python? Step by Step Guide

How to Iterate through Dictionary in Python? Step by Step Guide

Last updated:
22nd Apr, 2022
Read Time
6 Mins
share image icon
In this article
Chevron in toc
View All
How to Iterate through Dictionary in Python? Step by Step Guide

Data is stored efficiently in specific structures called data structures. It might also be described as the collection of data, the relationship between the data values, and the functions and operations that can be applied in the data structure. Python offers different data structures for the management of data. A user can store data, retrieve data, organize it, and also process the stored data. 

Of several data structures, the dictionary is one type of data structure used to store data in Python. The data within the dictionaries are stored in the form of pairs of key: values. There is no duplicate data inside a dictionary. Unlike the other data structures that store the data as a single value, the data values in a dictionary are stored as a map. Specific ways are required so that a user can iterate through the dictionary. 

In this article, the ways for iterating through a dictionary in Python will be explored. 

Check out our data science certifications to upskill yourself

Python dictionary 

The data structure “dictionary” in Python holds the data in an ordered collection. 

A dictionary can be created through the use of the curly brackets “{}.” The elements are placed within these brackets in a sequence. Elements placed within these brackets are separated through “comma.” The arrangement of the data in the dictionary is made in the form of a key and the corresponding element of pair as the value to the key, i.e., key: value.

These values that are stored to a key can be of any data type and also be duplicated. However, there is no duplication of the keys that makes them immutable. A special note should be made that the keys in the dictionary are case-sensitive. This means that the keys with the same name but with different cases will be treated as different.

The dictionaries are considered the cornerstone of the Python programming language as the classes, globals(), objects, modules, locals(), etc. are all built in the dictionaries. Therefore, a dictionary is a central structure in Python. 

A few critical points of the dictionaries are:

  • The keys are mapped to their values, and then they are stored in any collection or an array.
  • The keys that are in a dictionary should be hashable. This means that the keys in the dictionary should have a hash value that doesn’t change in a lifetime. 

For solving any problem in programming, dictionaries are used. Therefore, for a Python developer, dictionaries become a fundamental part of the developing kit.

Iteration through a dictionary in Python

In any sequence, the iteration is done through the use of integer indices. But unlike those sequences, the keys are used for indexing the dictionaries. These keys may be defined as a set of objects which are unique and hashable. And since the objects in the dictionaries have to be hashable; therefore, there are no mutable objects used as keys in the dictionaries. 

The iteration of the keys and the values in a Python iterate dictionary occurs in the same order as they were created in Python 3.6 and above. In comparison, it may not be the same in different versions of Python. In Python version 2.7, the dictionaries are represented as unordered structures. The item’s order is scrambled.

These data structures are widely used in Python. Also, iterating through a dictionary in Python is essential. A Python coder might face any scenarios where the iteration of the dictionaries is to be done for performing some actions over the key-value pairs in the dictionary. Therefore, it is vital to understand how a dictionary can be iterated in Python.

In Python, several ways can be considered, through which a user can iterate through dictionary Python.

  • Iterating through every key in the dictionary.
  • Iterating through all the values in a dictionary.
  • Iterating through all the pairs of key: values in the dictionary.

Our learners also read: Learn Python Online for Free

Iterating through every key in the dictionary

The dictionaries in Python can be represented as mapping objects. They inherit some special methods for performing certain operations by Python internally. For visualizing the attributes and the methods of Python objects, the dir() can be used. dir() is a function that is built-in inside Python. Running the function dir() with the argument of an empty dictionary, then all the attributes and methods being implemented by the dictionary will be shown.

Suppose the dictionary is included directly in a loop; in that case, the _iter_ method will be called by Python automatically, and then an iteration will be done over the dictionary’s keys. It is considered to be one of the easiest ways for iterating through a dictionary.

Check out all trending Python tutorial concepts in 2024.

Iterating through all the pairs of key: value in the dictionary

There might be scenarios where the user feels like working with both the keys and the values. In Python, the function items() can be used for iterating through a dictionary. The method returns a new view of the items of the dictionaries. 

The object view that is returned through the function items() generates the pairs of key-values only once at a single time, and this allows the iteration through the dictionary by the user in Python. 

Explore our Popular Data Science Online Certifications

Iterating a dictionary through keys

Working with keys in a dictionary provides the use of another function. This function that can be used is the .keys(). The function is used for returning a new object of view that contains the keys of the dictionary.

Iterating through the values

Apart from the keys, only the values can be used for iterating through a dictionary. A way that Python provides for iterating through values is by using the function .values(). The function returns a new view that holds the value present in a dictionary.

Using the function .values() will give access to the values of the dictionaries without any access to the keys. 

The article showed how the iteration could be carried out over a dictionary. Several other functions can also be carried out in the dictionary, such as modifying the values and the keys in the dictionary. A programmer can come up with many scenarios where these modifications might be required. Therefore, being an essential part of the programming language of Python, understanding the data structures seems to be necessary also if the language application depends on the Data science field where the user has to deal with a lot of data. 

Top Data Science Skills You Should Learn

upGrad’s Exclusive Data Science Webinar for you –

Watch our Webinar on The Future of Consumer Data in an Open Data Economy

Read our popular Data Science Articles


If python programming is your choice and you want to explore the data science area through Python, you can check the Executive PG Programme in Data Science course offered by upGrad. If you are a working professional, then the course will suit you best. More information regarding the course can be explored on the course website. For any queries, our team of assistance is ready to help you.


Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.

Frequently Asked Questions (FAQs)

1What is a dictionary in Python?

Python dictionaries are a type of data structure that stores, manages, and processes data in key:value pairs.

2Are the data in the Python dictionary ordered?

The Dictionary data in the python version 3.6 and above stored the data in an ordered manner. However, the data in Python version 2.7 was unordered.

3Why are Python dictionaries important?

The Python dictionaries seem essential because the modules, objects, classes, etc., are all built on the dictionaries. Therefore, it is a fundamental part of the Python programming language.

Explore Free Courses

Suggested Blogs

Linear Search vs Binary Search: Difference Between Linear Search & Binary Search
In my journey through data structures, I’ve navigated the nuances of linear search vs binary search in data structure, especially when dealing w
Read More

by Rohit Sharma

23 Jun 2024

Information Retrieval System Explained: Types, Comparison & Components
An Information Retrieval System is a set of algorithms that facilitate the relevance of displayed documents to searched queries. In simple words, it w
Read More

by Rohit Sharma

18 Jun 2024

4 Types of Trees in Data Structures Explained: Properties & Applications
In this article, you will learn about the Types of Trees in Data Structures with examples, Properties & Applications. In my journey with data stru
Read More

by Rohit Sharma

31 May 2024

Searching in Data Structure: Different Search Methods Explained
The communication network is expanding, and so the people are using the internet! Businesses are going digital for efficient management. The data gene
Read More

by Rohit Sharma

29 May 2024

What is Linear Data Structure? List of Data Structures Explained
Data structures are the data structured in a way for efficient use by the users. As the computer program relies hugely on the data and also requires a
Read More

by Rohit Sharma

28 May 2024

4 Types of Data: Nominal, Ordinal, Discrete, Continuous
Summary: In this Article, you will learn about what are the 4 Types of Data in Statistics. Qualitative Data Type Nominal Ordinal Quantitative Data
Read More

by Rohit Sharma

28 May 2024

Python Developer Salary in India in 2024 [For Freshers & Experienced]
Wondering what is the range of Python developer salary in India? Before going deep into that, do you know why Python is so popular now? Python has be
Read More

by Sriram

21 May 2024

Binary Tree in Data Structure: Properties, Types, Representation & Benefits
Data structures serve as the backbone of efficient data organization and management within computer systems. They play a pivotal role in computer algo
Read More

by Rohit Sharma

21 May 2024

Data Analyst Salary in India in 2024 [For Freshers & Experienced]
Summary: In this Article, you will learn about Data Analyst Salary in India in 2024. Data Science Job roles Average Salary per Annum Data Scient
Read More

by Shaheen Dubash

20 May 2024

Schedule 1:1 free counsellingTalk to Career Expert
footer sticky close icon