Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Initialize Python Dictionary with Keys and Values
Next article icon

Iterate Through Dictionary Keys And Values In Python

Last Updated : 15 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, a Dictionary is a data structure where the data will be in the form of key and value pairs. So, to work with dictionaries we need to know how we can iterate through the keys and values. In this article, we will explore different approaches to iterate through keys and values in a Dictionary in Python.

Iterate Through Dictionary Keys And Values in Python

Below, are the possible approaches to Iterate Through Dictionary Keys And Values In Python.

  • Using list comprehension
  • Using keys() and values() methods
  • Using the zip() method

Iterate Through a Dictionary Using List Comprehension

The below approach code uses list comprehension and the items() method on a dictionary in Python to iterate through its keys and values. The for loop in list comprehension iterates through all the keys and values and prints all the key-value pairs in a dictionary format.

Python3
# defining dictionary related to GeeksforGeeks geeks_data = {'language': 'Python', 'framework': 'Django', 'topic': 'Data Structures'}  keys = [key for key in geeks_data.keys()] values = [value for value in geeks_data.values()]  # Print keys print("Keys:") for key in keys:     print(key)  # Print values print("\nValues:") for value in values:     print(value) 

Output
Keys: language framework topic  Values: Python Django Data Structures 

Iterate Through a Dictionary Using keys() and values() Methods

The below approach code uses keys( ) and values() methods to iterate through all the keys and values of the dictionary (d). The d.keys( ) returns all the keys whereas the values() method returns all the values of the dictionary. The for loop iterates through all the keys and prints all the keys and values.

Python3
# defining dictionary related to GeeksforGeeks geeks_data = {'language': 'Python', 'framework': 'Django', 'topic': 'Data Structures'}  # Iterate through keys print("Keys:") for key in geeks_data.keys():     print(key)  # Iterate through values print("\nValues:") for value in geeks_data.values():     print(value) 

Output
Keys: language framework topic  Values: Python Django Data Structures 

Iterate Through a Dictionary Using zip( ) Function

The belew approach code defines a dictionary and then uses the zip() method to unzip the keys and values. Finally, it prints the keys and values separately using list comprehensions.

Python3
# defining dictionary related to GeeksforGeeks geeks_data = {'language': 'Python',               'framework': 'Django', 'topic': 'Data Structures'}  keys, values = zip(*geeks_data.items())  print("Keys:") _ = [print(key) for key in keys]  print("\nValues:") _ = [print(value) for value in values] 

Output
Keys: language framework topic  Values: Python Django Data Structures 

Next Article
Initialize Python Dictionary with Keys and Values

M

mohithsharmajf3o
Improve
Article Tags :
  • Python
  • Python Programs
  • python-dict
Practice Tags :
  • python
  • python-dict

Similar Reads

  • Python | Iterate through value lists dictionary
    While working with dictionary, we can have a case in which we need to iterate through the lists, which are in the keys of dictionaries. This kind of problem can occur in web development domain. Let's discuss certain ways in which this problem can be solved. Method #1: Using list comprehension List c
    4 min read
  • Python - Assign values to initialized dictionary keys
    Sometimes, while working with python dictionaries, we can have a problem in which we need to initialize dictionary keys with values. We save a mesh of keys to be initialized. This usually happens during web development while working with JSON data. Lets discuss certain ways in which this task can be
    5 min read
  • Python Iterate Dictionary Key, Value
    In Python, a Dictionary is a data structure that stores the data in the form of key-value pairs. It is a mutable (which means once created we modify or update its value later on) and unordered data structure in Python. There is a thing to keep in mind while creating a dictionary every key in the dic
    3 min read
  • Iterate Through Specific Keys in a Dictionary in Python
    Sometimes we need to iterate through only specific keys in a dictionary rather than going through all of them. We can use various methods to iterate through specific keys in a dictionary in Python. Using dict.get() MethodWhen we're not sure whether a key exists in the dictionary and don't want to ra
    3 min read
  • Initialize Python Dictionary with Keys and Values
    In this article, we will explore various methods for initializing Python dictionaries with keys and values. Initializing a dictionary is a fundamental operation in Python, and understanding different approaches can enhance your coding efficiency. We will discuss common techniques used to initialize
    3 min read
  • Python - Iterate over Tuples in Dictionary
    In this article, we will discuss how to Iterate over Tuples in Dictionary in Python. Method 1: Using index We can get the particular tuples by using an index: Syntax: dictionary_name[index] To iterate the entire tuple values in a particular index for i in range(0, len(dictionary_name[index])): print
    2 min read
  • Python Dictionary Add Value to Existing Key
    The task of adding a value to an existing key in a Python dictionary involves modifying the value associated with a key that is already present. Unlike adding new key-value pairs, this operation focuses on updating the value of an existing key, allowing us to increment, concatenate or otherwise adju
    2 min read
  • Intersect Two Dictionaries through Keys - Python
    The task is to intersect two dictionaries through their keys, which means finding the keys that are common to both dictionaries. For example, given the dictionaries a = {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} and b = {'akshat': 15, 'nikhil': 1, 'me': 56}, the goal is to find the inter
    4 min read
  • Add a key value pair to Dictionary in Python
    The task of adding a key-value pair to a dictionary in Python involves inserting new pairs or updating existing ones. This operation allows us to expand the dictionary by adding new entries or modify the value of an existing key. For example, starting with dictionary d = {'key1': 'geeks', 'key2': 'f
    3 min read
  • Python program to Swap Keys and Values in Dictionary
    Dictionary is quite a useful data structure in programming that is usually used to hash a particular key with value so that they can be retrieved efficiently. Let’s discuss various ways of swapping the keys and values in Python Dictionary.  Method#1 (Does not work when there are multiple same values
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences