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:
Appending a Dictionary to a List in Python
Next article icon

Create a Dictionary with List Comprehension in Python

Last Updated : 08 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of creating a dictionary with list comprehension in Python involves iterating through a sequence and generating key-value pairs in a concise manner. For example, given two lists, keys = [“name”, “age”, “city”] and values = [“Alice”, 25, “New York”], we can pair corresponding elements using list comprehension and convert them into a dictionary, resulting in {‘name’: ‘Alice’, ‘age’: 25, ‘city’: ‘New York’}.

Using zip()

zip() pairs elements from two lists and is commonly used to create dictionaries efficiently. By wrapping zip() inside a list comprehension and converting it into a dictionary using dict(), we can merge two lists into key-value pairs. It ensures that elements are mapped correctly without extra iterations.

Python
keys = ["name", "age", "city"] values = ["Alice", 25, "New York"]  d = dict([(k, v) for k, v in zip(keys, values)]) print(d) 

Output
{'name': 'Alice', 'age': 25, 'city': 'New York'} 

Explanation: zip(keys, values) pairs elements into tuples like [(‘name’, ‘Alice’), (‘age’, 25), (‘city’, ‘New York’)]. Then, list comprehension generates a list of key-value pairs, which dict() converts into a dictionary, storing it in d.

Using dict()

Dictionary can be created by passing a list of tuples to the dict(). Using list comprehension, we first generate a list of tuples, where each tuple consists of a key-value pair. This method is useful when keys and values are derived through computations, such as squaring numbers or mapping values dynamically.

Python
a = [(x, x**2) for x in range(1, 6)] d = dict(a) print(d) 

Output
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25} 

Explanation: List comprehension generates a list of tuples, where each tuple consists of a number x (from 1 to 5) as the first element and its square (x**2) as the second. Then, this list of tuples is converted into a dictionary using dict().

Using enumerate()

enumerate() assigns an index to each element in an iterable, making it useful for generating dictionaries with numeric keys. By applying list comprehension, we can create a list of (index, value) pairs and convert it into a dictionary. It ensures a structured and ordered mapping of elements.

Python
a = ["apple", "banana", "cherry"]  d = dict([(idx, val) for idx, val in enumerate(a)]) print(d) 

Output
{0: 'apple', 1: 'banana', 2: 'cherry'} 

Explanation: List comprehension iterates over enumerate(a), assigning an index to each element in a, creating tuples (idx, val). These tuples are then passed to dict(), converting them into a dictionary where indexes are keys and list elements are values.



Next Article
Appending a Dictionary to a List in Python
author
sravankumar_171fa07058
Improve
Article Tags :
  • Python
  • Python Programs
  • Python dictionary-programs
  • python-dict
Practice Tags :
  • python
  • python-dict

Similar Reads

  • Convert a Dictionary to a List in Python
    In Python, dictionaries and lists are important data structures. Dictionaries hold pairs of keys and values, while lists are groups of elements arranged in a specific order. Sometimes, you might want to change a dictionary into a list, and Python offers various ways to do this. How to Convert a Dict
    3 min read
  • Appending a Dictionary to a List in Python
    Appending a dictionary allows us to expand a list by including a dictionary as a new element. For example, when building a collection of records or datasets, appending dictionaries to a list can help in managing data efficiently. Let's explore different ways in which we can append a dictionary to a
    3 min read
  • Convert a List to Dictionary Python
    We are given a list we need to convert the list in dictionary. For example, we are given a list a=[10,20,30] we need to convert the list in dictionary so that the output should be a dictionary like {0: 10, 1: 20, 2: 30}. We can use methods like enumerate, zip to convert a list to dictionary in pytho
    2 min read
  • Python Create Dictionary with Integer
    The task of creating a dictionary from a list of keys in Python, where each key is assigned a unique integer value, involves transforming the list into a dictionary. Each element in the list becomes a key and the corresponding value is typically its index or a different integer. For example, if we h
    3 min read
  • Create Dictionary from the List-Python
    The task of creating a dictionary from a list in Python involves mapping each element to a uniquely generated key, enabling structured data storage and quick lookups. For example, given a = ["gfg", "is", "best"] and prefix k = "def_key_", the goal is to generate {'def_key_gfg': 'gfg', 'def_key_is':
    3 min read
  • Python - Accessing Items in Lists Within Dictionary
    Given a dictionary with values as a list, the task is to write a python program that can access list value items within this dictionary. Method 1: Manually accessing the items in the list This is a straightforward method, where the key from which the values have to be extracted is passed along with
    5 min read
  • Python | Dictionary creation using list contents
    Sometimes we need to handle the data coming in the list format and convert list into dictionary format. This particular problem is quite common while we deal with Machine Learning to give further inputs in changed formats. Let's discuss certain ways in which this inter conversion happens. Method #1
    5 min read
  • Python - Convert Index Dictionary to List
    Sometimes, while working with Python dictionaries, we can have a problem in which we have keys mapped with values, where keys represent list index where value has to be placed. This kind of problem can have application in all data domains such as web development. Let's discuss certain ways in which
    3 min read
  • Convert Dictionary to String List in Python
    The task of converting a dictionary to a string list in Python involves transforming the key-value pairs of the dictionary into a formatted string and storing those strings in a list. For example, consider a dictionary d = {1: 'Mercedes', 2: 'Audi', 3: 'Porsche', 4: 'Lambo'}. Converting this to a st
    3 min read
  • Python List Comprehension With Two Lists
    List comprehension allows us to generate new lists based on existing ones. Sometimes, we need to work with two lists together, performing operations or combining elements from both. Let's explore a few methods to use list comprehension with two lists. Using zip() with List ComprehensionOne of the mo
    3 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