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:
Python - Append Multitype Values in Dictionary
Next article icon

Append Dictionary Keys and Values ( In order ) in Dictionary – Python

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

Appending dictionary keys and values in order ensures that the sequence in which they are added is preserved. For example, when working with separate lists of keys and values, we might want to append them in a specific order to build a coherent dictionary. Let’s explore several methods to achieve this.

Using zip and Dictionary Constructor

This is the most efficient and commonly used method to append keys and values in order.

Python
# Initialize lists of keys and values keys = ["name", "age", "city"] values = ["Alice", 30, "New York"]  # Create a dictionary by zipping keys and values d = dict(zip(keys, values))  # Print the dictionary print(d) 

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

Explanation:

  • zip() function pairs each key with its corresponding value in a single step.
  • dict() constructor efficiently creates a dictionary from the paired elements.
  • This method is highly efficient because it combines operations into one concise line.

Using for Loop with Direct Assignment

This method involves manually iterating over the keys and values to append them in order.

Python
# Initialize lists of keys and values keys = ["name", "age", "city"] values = ["Alice", 30, "New York"]  # Initialize an empty dictionary d = {}  # Append keys and values in order for k, v in zip(keys, values):     d[k] = v  # Print the dictionary print(d) 

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

Explanation:

  • zip() function combines keys and values for iteration.
  • Each key-value pair is appended to the dictionary using direct assignment.

Using update() with a Dictionary Comprehension

This method uses a dictionary comprehension to create key-value pairs and appends them to an existing dictionary using the update method.

Python
# Initialize lists of keys and values keys = ["name", "age", "city"] values = ["Alice", 30, "New York"]  # Initialize an empty dictionary d = {}  # Append keys and values using dictionary comprehension and update d.update({k: v for k, v in zip(keys, values)})  # Print the dictionary print(d) 

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

Explanation:

  • A dictionary comprehension generates the key-value pairs from the zip output.
  • The update method appends these pairs to the dictionary.
  • This method is useful for situations where keys and values are generated dynamically.

Using OrderedDict from collections

This method is useful if maintaining the order is critical, especially when working with Python versions prior to 3.7.

Python
from collections import OrderedDict  # Initialize lists of keys and values keys = ["name", "age", "city"] values = ["Alice", 30, "New York"]  # Create an OrderedDict by zipping keys and values d = OrderedDict(zip(keys, values))  # Print the dictionary print(d) 

Output
OrderedDict({'name': 'Alice', 'age': 30, 'city': 'New York'}) 

Explanation:

  • OrderedDict() ensures that the keys are stored in the order they are inserted.
  • It behaves like a regular dictionary but provides guaranteed order preservation.
  • While slightly less efficient than standard dictionaries, it is valuable in older Python versions.

Using a List of Tuples and dict Constructor

This method creates a list of tuples representing key-value pairs and converts it to a dictionary.

Python
# Initialize lists of keys and values keys = ["name", "age", "city"] values = ["Alice", 30, "New York"]  # Create a dictionary using a list of tuples d = dict([(k, v) for k, v in zip(keys, values)])  # Print the dictionary print(d) 

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

Explanation:

  • A list of key-value tuples is generated using a list comprehension.
  • The dict constructor converts the list of tuples into a dictionary.
  • This method is less efficient due to the intermediate creation of a list.


Next Article
Python - Append Multitype Values in Dictionary
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python dictionary-programs
Practice Tags :
  • python

Similar Reads

  • Python - Add custom values key in List of dictionaries
    The task of adding custom values as keys in a list of dictionaries involves inserting a new key-value pair into each dictionary within the list. In Python, dictionaries are mutable, meaning the key-value pairs can be modified or added easily. When working with a list of dictionaries, the goal is to
    5 min read
  • Regular Dictionary vs Ordered Dictionary in Python
    Dictionary in Python is an unordered collection of data values, used to store data values like a map, unlike other Data Types that hold only a single value as an element, a Dictionary holds key: value pair. Key-value is provided in the dictionary to make it more optimized. A regular dictionary type
    5 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
  • Convert Dictionary Value list to Dictionary List Python
    Sometimes, while working with Python Dictionaries, we can have a problem in which we need to convert dictionary list to nested records dictionary taking each index of dictionary list value and flattening it. This kind of problem can have application in many domains. Let's discuss certain ways in whi
    9 min read
  • Python - Append Multitype Values in Dictionary
    There are cases where we may want to append multiple types of values, such as integers, strings or lists to a single dictionary key. For example, if we are creating a dictionary to store multiple types of data under the same key, such as user details (e.g., age, address, and hobbies), we need to han
    2 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 - Convert list of dictionaries to Dictionary Value list
    We are given a list of dictionaries we need to convert it to dictionary. For example, given a list of dictionaries: d = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6}], the output should be: {'a': [1, 3, 5], 'b': [2, 4, 6]}. Using Dictionary ComprehensionUsing dictionary comprehension, we can
    3 min read
  • Python Print Dictionary Keys and Values
    When working with dictionaries, it's essential to be able to print their keys and values for better understanding and debugging. In this article, we'll explore different methods to Print Dictionary Keys and Values. Example: Using print() Method [GFGTABS] Python my_dict = {'a': 1, 'b'
    2 min read
  • Get Index of Values in Python Dictionary
    Dictionary values are lists and we might need to determine the position (or index) of each element within those lists. Since dictionaries themselves are unordered (prior to Python 3.7) or ordered based on insertion order (in Python 3.7+), the concept of "index" applies to the values—specifically whe
    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