Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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:
Create Nested Dictionary using given List - Python
Next article icon

Create Nested Dictionary using given List - Python

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

The task of creating a nested dictionary in Python involves pairing the elements of a list with the key-value pairs from a dictionary. Each key from the list will map to a dictionary containing a corresponding key-value pair from the original dictionary. For example, given the dictionary a = {'Gfg': 4, 'is': 5, 'best': 9} and the list b = [8, 3, 2], the task is to create a nested dictionary like this:{8: {'Gfg': 4}, 3: {'is': 5}, 2: {'best': 9}} .

Using zip()

zip() pair elements from two or more iterables. In this case, it pairs the elements of the list with the key-value pairs from the dictionary. After pairing the elements, We can create a nested dictionary by iterating over the zipped pairs.

Python
a = {'Gfg': 4, 'is': 5, 'best': 9} b = [8, 3, 2]  res = {key: {k: v} for key, (k, v) in zip(b, a.items())} print(res) 

Output
{8: {'Gfg': 4}, 3: {'is': 5}, 2: {'best': 9}} 

Explanation: zip(b, a.items()) pairs each element of b with a key-value pair from a, assigning the list element to key and the dictionary pair to (k, v). The comprehension {key: {k: v}} then creates a nested dictionary where each key from b maps to its corresponding {k: v} from a.

Using for loop

This method also uses the zip() function, but the key difference is that the looping process is slightly more explicit and manual. Here, we directly iterate over the zipped elements without using dictionary comprehension.

Python
a = {'Gfg': 4, 'is': 5, 'best': 9} b = [8, 3, 2]  res = {} for key, (k, v) in zip(b, a.items()):     res[key] = {k: v}      print(res) 

Output
{8: {'Gfg': 4}, 3: {'is': 5}, 2: {'best': 9}} 

Explanation: for loop uses zip(b, a.items()) to pair each element from b with a key-value pair from a. For each pair, res[key] = {k: v} assigns a nested dictionary {k: v} to key, creating res where each key from b maps to the corresponding key-value pair from a.

Using dict()

While this method uses zip() to pair the list and dictionary items, it applies the dict() constructor to explicitly create the final nested dictionary. In this case, the dict() constructor is used with a lambda function to wrap each value in another dictionary.

Python
a = {'Gfg': 4, 'is': 5, 'best': 9} b = [8, 3, 2]  res = dict(map(lambda key_val: (key_val[0], {key_val[1][0]: key_val[1][1]}), zip(b, a.items()))) print(res) 

Output
{8: {'Gfg': 4}, 3: {'is': 5}, 2: {'best': 9}} 

Explanation: zip(b, a.items()) to pair elements from b with key-value pairs from a. The map with lambda creates tuples where each element from b is a key, and its value is a nested dictionary from a. dict() then converts these tuples into the final dictionary res.


Next Article
Create Nested Dictionary using given List - Python

M

manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
  • Python dictionary-programs
Practice Tags :
  • python

Similar Reads

    Convert Nested Dictionary to List in Python
    In this article, we’ll explore several methods to Convert Nested Dictionaries to a List in Python. List comprehension is the fastest and most concise way to convert a nested dictionary into a list.Pythona = { "a": {"x": 1, "y": 2}, "b": {"x": 3, "y": 4}, } # Convert nested dictionary to a list of li
    3 min read
    Python - Create a Dictionary using List with None Values
    The task of creating a dictionary from a list of keys in Python involves transforming a list of elements into a dictionary where each element becomes a key. Each key is typically assigned a default value, such as None, which can be updated later. For example, if we have a list like ["A", "B", "C"],
    3 min read
    Load CSV data into List and Dictionary using Python
    Prerequisites: Working with csv files in Python  CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or m
    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
    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
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