• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Dictionary / Iterating over dictionary in Python

Iterating over dictionary in Python

Author: Aditya Raj
Last Updated: December 3, 2021

Dictionaries are one of the most frequently used data structures in python. It contains data in the form of key value pairs. While processing the data with dictionaries, we may need to iterate over the items in the dictionary to change the values or read the values present in the dictionary. In this article, we will see various ways for iterating over dictionary in python.

Iterating over a dictionary using for loop

As we iterate through lists or tuples using for loops in python, we can also iterate  through a python dictionary using a for loop.

When we try to iterate over a dictionary using for loop,it implicitly calls __iter__() method. The __iter__() method returns an iterator with the help of which we can iterate over the entire dictionary. As we know that dictionaries in python are indexed using keys, the iterator returned by __iter__() method iterates over the keys in the python dictionary.

So, with a for loop, we can iterate over and access all the keys of a dictionary as follows.

myDict={"name":"PythonForBeginners","acronym":"PFB","about":"Python Tutorials Website"}
print("The dictionary is:")
print(myDict)
print("The keys in the dictionary are:")
for x in myDict:
    print(x)

Output:

The dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB', 'about': 'Python Tutorials Website'}
The keys in the dictionary are:
name
acronym
about

In the output we can see that all the the all the keys have been printed. In the for loop the iterator x iterates over all the keys in the dictionary which are then printed.

Having obtained the keys of the dictionary using for loop, we can also iterate over the values in the dictionary as follows.

myDict={"name":"PythonForBeginners","acronym":"PFB","about":"Python Tutorials Website"}
print("The dictionary is:")
print(myDict)
print("The values in the dictionary are:")
for x in myDict:
    print(myDict[x])

Output:

The dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB', 'about': 'Python Tutorials Website'}
The values in the dictionary are:
PythonForBeginners
PFB
Python Tutorials Website

In the code above, we have simply obtained an iterator which iterates over the keys in the and then we have accessed the values associated to the keys using the syntax dict_name[key_name] and then the values are printed.

Iterate over keys of a  dictionary

We can use the keys() method to iterate over keys of a dictionary. The keys() method returns a list of keys in the dictionary when invoked on a dictionary and then we can iterate over the list to access the keys in the dictionary as follows.

myDict={"name":"PythonForBeginners","acronym":"PFB","about":"Python Tutorials Website"}
print("The dictionary is:")
print(myDict)
print("The keys in the dictionary are:")
keyList=myDict.keys()
for x in keyList:
    print(x)

Output:

The dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB', 'about': 'Python Tutorials Website'}
The keys in the dictionary are:
name
acronym
about

We can also access values associated with the keys of the dictionary once we have the keys in the list as follows.

myDict={"name":"PythonForBeginners","acronym":"PFB","about":"Python Tutorials Website"}
print("The dictionary is:")
print(myDict)
print("The values in the dictionary are:")
keyList=myDict.keys()
for x in keyList:
    print(myDict[x])

Output:

The dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB', 'about': 'Python Tutorials Website'}
The values in the dictionary are:
PythonForBeginners
PFB
Python Tutorials Website

Iterate over values of a dictionary in python

If we only want to access the values in the dictionary, we can do so with the help of the values() method.The values() method when invoked on a dictionary returns a list of all the values present in the dictionary. We can access the values in the dictionary using values() method and for loop as follows.

myDict={"name":"PythonForBeginners","acronym":"PFB","about":"Python Tutorials Website"}
print("The dictionary is:")
print(myDict)
print("The values in the dictionary are:")
valueList=myDict.values()
for x in valueList:
    print(x)

Output:

The dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB', 'about': 'Python Tutorials Website'}
The values in the dictionary are:
PythonForBeginners
PFB
Python Tutorials Website

In the output, we can see that all the values present in the dictionary are printed one by one.

Iterating over items in a dictionary in python

We can iterate over and access the key value pairs using the items() method. The items() method when invoked on a dictionary, returns a list of tuples which have keys and values as pairs. Each tuple has a key on its 0th index and the value associated with the key is present on the 1st index of the tuple. We can access the key value pairs using items() method as shown below.

myDict={"name":"PythonForBeginners","acronym":"PFB","about":"Python Tutorials Website"}
print("The dictionary is:")
print(myDict)
print("The items in the dictionary are:")
itemsList=myDict.items()
for x in itemsList:
    print(x)

Output:

The dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB', 'about': 'Python Tutorials Website'}
The items in the dictionary are:
('name', 'PythonForBeginners')
('acronym', 'PFB')
('about', 'Python Tutorials Website')

Along with iterating the items in the dictionary using the items() method,we can also iterate over keys and values of  a dictionary using two iterators in the for loop as follows.

myDict={"name":"PythonForBeginners","acronym":"PFB","about":"Python Tutorials Website"}
print("The dictionary is:")
print(myDict)
itemList=myDict.items()
print("The key value pairs in the dictionary are:")
for x,y in itemList:
    print(x,end=":")
    print(y)

Output:

The dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB', 'about': 'Python Tutorials Website'}
The key value pairs in the dictionary are:
name:PythonForBeginners
acronym:PFB
about:Python Tutorials Website

In the above program, the first iterator iterates over the keys in the dictionary and the second iterator iterates over the respective values associated with those keys which are present in the form of tuple containing key value pairs in the list returned by items() method. 

Conclusion

In this article, we have seen various ways to iterate over the data in dictionaries. We have seen how to access the keys and values of the dictionary using for loop and inbuilt methods like keys(), values() and items(). We can also write the programs used in this article with exception handling using python try except to make the programs more robust and handle errors in a systematic way. Stay tuned for more informative articles.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Dictionary Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us