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:
Dictionary keys as a list in Python
Next article icon

Initialize a Dictionary with Only Keys from a List – Python

Last Updated : 28 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of initializing a dictionary with only keys from a list in Python involves creating a dictionary where the keys are derived from the elements of the list and each key is assigned a default value .

For example, given a list like [“A”, “B”, “C”], the goal is to transform this list into a dictionary where the keys are “A”, “B”, and “C”, all mapped to the default value None. After initialization, the dictionary will look like this: {‘A’: None, ‘B’: None, ‘C’: None}. 

Using dictionary comprehension

Dictionary comprehension is one of the most efficient way to initialize a dictionary when the keys are already available in a list . It allows us to construct a dictionary in a single, concise statement, where each key in the list is mapped to a specified value. This method combines clarity and performance, making it a favorite choice for many Python developers.

Python
li = ["Paras", "Jain", "Cyware"] d = {key: None for key in li}  print(d) 

Output
{'Paras': None, 'Jain': None, 'Cyware': None} 

Explanation: {key: None for key in li} loops over each element in li, uses each element as a dictionary key and assigns None as the corresponding value. This process results in a new dictionary with the keys from the list and None as their values .

Table of Content

  • Using dict.fromkeys()
  • Using map()
  • Using for loop

Using dict.fromkeys()

fromkeys() method is another efficient approach in Python to initialize a dictionary from a list of keys. This method is specifically designed to handle cases like this, where we want to initialize all keys with the same value. It’s both concise and built for this exact scenario, making it a good alternative to dictionary comprehension.

Python
li = ["Paras", "Jain", "Cyware"] d = dict.fromkeys(li)  print(d) 

Output
{'Paras': None, 'Jain': None, 'Cyware': None} 

Explanation: dict.fromkeys(li) creates a dictionary with keys from li, and assigns None as the value for each key.

Using map()

Another method to initialize a dictionary is by using the map() in combination with the dict() constructor. While this approach is less common for this specific task, it is still an option. map() function transforms the list into key-value pairs and dict() then converts it into a dictionary.

Python
li = ["Paras", "Jain", "Cyware"] d = dict(map(lambda key: (key, None), li))  print(d) 

Output
{'Paras': None, 'Jain': None, 'Cyware': None} 

Explanation: map() applies the lambda function to each element of the list li, creating tuples of each element paired with None. dict() constructor then converts these tuples into a dictionary where each element of the list becomes a key, and the value for each key is None.

Using for loop

for loop is the most basic way to initialize a dictionary from a list of keys. It’s highly efficient , making it a good option for beginners. However, it’s not efficient as the methods mentioned above.

Python
li = ["Paras", "Jain", "Cyware"] d = {} # initialize empty dictionary  for key in li:     d.setdefault(key, None) print(d) 

Output
{'Paras': None, 'Jain': None, 'Cyware': None} 

Explanation: setdefault() checks if a key exists in the dictionary. If not, it adds the key with a default value None .For each item in the list li, setdefault(key, None) is called, adding each key with None as the value.



Next Article
Dictionary keys as a list in Python
author
everythingispossible
Improve
Article Tags :
  • Python
  • Python Programs
  • Python dictionary-programs
  • python-dict
Practice Tags :
  • python
  • python-dict

Similar Reads

  • Python | Initialize dictionary with multiple keys
    Sometimes, while working with dictionaries, we might have a problem in which we need to initialize the dictionary with more than one key with the same value. This application requirement can be in domains of web development in which we might want to declare and initialize simultaneously. Let's discu
    8 min read
  • Python - Initialize dictionary keys with Matrix
    Sometimes, while working with Python Data, we can have a problem in which we need to construct an empty mesh of dictionaries for further population of data. This problem can have applications in many domains which include data manipulation. Let's discuss certain ways in which this task can be perfor
    4 min read
  • Python | Initialize list with empty dictionaries
    While working with Python, we can have a problem in which we need to initialize a list of a particular size with empty dictionaries. This task has it's utility in web development to store records. Let's discuss certain ways in which this task can be performed. Method #1 : Using {} + "*" operator Thi
    5 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
  • Dictionary keys as a list in Python
    In Python, we will encounter some situations where we need to extract the keys from a dictionary as a list. In this article, we will explore various easy and efficient methods to achieve this. Using list() The simplest and most efficient way to convert dictionary keys to lists is by using a built-in
    2 min read
  • Get all Unique Keys from a List of Dictionaries - Python
    Our task is to get all unique keys from a list of dictionaries and we are given a list where each element is a dictionary, we need to extract and return a list of keys that appear across all dictionaries. The result should contain each key only once regardless of how many times it appears. For examp
    3 min read
  • Python - Initialize dictionary with custom value list
    In python one usually comes across situations in which one has to use dictionary for storing the lists. But in those cases, one usually checks for first element and then creates a list corresponding to key when it comes. But its always wanted a method to initialize the dict. keys with a custom list.
    4 min read
  • Python | Initialize dictionary with None values
    Sometimes, while working with dictionaries, we might have a utility in which we need to initialize a dictionary with None values so that they can be altered later. This kind of application can occur in cases of memoization in general or competitive programming. Let's discuss certain ways in which th
    4 min read
  • How to Initialize a Dictionary in Python Using For Loop
    When you want to create a dictionary with the initial key-value pairs or when you should transform an existing iterable, such as the list into it. You use string for loop initialization. In this article, we will see the initialization procedure of a dictionary using a for loop. Initialize Python Dic
    3 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
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