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:
Add Same Key in Python Dictionary
Next article icon

Add a key value pair to Dictionary in Python

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

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’: ‘for’}, we add multiple key-value pairs at once: ‘key3’: ‘Geeks’, ‘key4’: ‘is’, ‘key5’: ‘portal’, and ‘key6’: ‘Computer’. After the update, the dictionary becomes {‘key1’: ‘geeks’, ‘key2’: ‘for’, ‘key3’: ‘Geeks’, ‘key4’: ‘is’, ‘key5’: ‘portal’, ‘key6’: ‘Computer’}.

Using update()

update() is used when we need to add multiple key-value pairs or modify existing ones. We can pass another dictionary or an iterable of key-value pairs to this method and it will add the new pairs or update the existing keys.

Python
d = {'key1': 'geeks', 'key2': 'for'}  d.update({'key3': 'Geeks', 'key4': 'is', 'key5': 'portal', 'key6': 'Computer'}) print(d) 

Output
{'key1': 'geeks', 'key2': 'for', 'key3': 'Geeks', 'key4': 'is', 'key5': 'portal', 'key6': 'Computer'} 

Explanation: update() adds key-value pairs to d, updating existing keys and adding new ones .

Table of Content

  • Using square brackets []
  • Using dictionary unpacking
  • Using dict()

Using square brackets []

This is the simplest way to add or update a key-value pair in a dictionary. We access the dictionary by specifying the key inside square brackets and assign the corresponding value. If the key doesn’t exist, it will be added to the dictionary.

Python
d = {'key1': 'geeks', 'key2': 'for'}  d['key3'] = 'Geeks' d['key4'] = 'is' d['key5'] = 'portal' d['key6'] = 'Computer' print(d) 

Output
{'key1': 'geeks', 'key2': 'for', 'key3': 'Geeks', 'key4': 'is', 'key5': 'portal', 'key6': 'Computer'} 

Explanation: It adds new key-value pairs to d using direct assignment.

Using dictionary unpacking

dictionary unpacking allows us to merge dictionaries or add new key-value pairs in a concise way. By using the unpacking operator **, we can merge an existing dictionary with new items in a single statement.

Python
d = {'key1': 'geeks', 'key2': 'for'}  d = {**d, 'key3': 'Geeks', 'key4': 'is', 'key5': 'portal', 'key6': 'Computer'} print(d) 

Output
{'key1': 'geeks', 'key2': 'for', 'key3': 'Geeks', 'key4': 'is', 'key5': 'portal', 'key6': 'Computer'} 

Explanation: **d unpacks the original dictionary d , and new key-value pairs are added.

Using dict()

dict() allows us to create a new dictionary by combining existing ones with new key-value pairs. This method is useful when we want to create a new dictionary that includes both the original and new items.

Python
d = {'key1': 'geeks', 'key2': 'for'}  d = dict(d, key3='Geeks', key4='is', key5='portal', key6='Computer') print(d) 

Output
{'key1': 'geeks', 'key2': 'for', 'key3': 'Geeks', 'key4': 'is', 'key5': 'portal', 'key6': 'Computer'} 

Explanation: dict(d) creates a new dictionary from d and new key-value pairs are added using keyword arguments.



Next Article
Add Same Key in Python Dictionary

S

Shivam_k
Improve
Article Tags :
  • Python
  • Python Programs
  • Python dictionary-programs
  • python-dict
Practice Tags :
  • python
  • python-dict

Similar Reads

  • 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
  • Add Same Key in Python Dictionary
    The task of adding the same key in a Python dictionary involves updating the value of an existing key rather than inserting a new key-value pair. Since dictionaries in Python do not allow duplicate keys, adding the same key results in updating the value of that key. For example, consider a dictionar
    3 min read
  • Python - Add Values to Dictionary of List
    A dictionary of lists allows storing grouped values under specific keys. For example, in a = {'x': [10, 20]}, the key 'x' maps to the list [10, 20]. To add values like 30 to this list, we use efficient methods to update the dictionary dynamically. Let’s look at some commonly used methods to efficien
    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
  • Python - Mapping Key Values to Dictionary
    We are given two list and we need to map key to values of another list so that it becomes dictionary. For example, we are given two list k = ['a', 'b', 'c'] and v = [1, 2, 3] we need to map the keys of list k to the values of list v so that the resultant output should be {'a': 1, 'b': 2, 'c': 3}. Us
    3 min read
  • Add Key to Dictionary Python Without Value
    In Python, dictionaries are a versatile and widely used data structure that allows you to store and organize data in key-value pairs. While adding a key to a dictionary is straightforward when assigning a value, there are times when you may want to add a key without specifying a value initially. In
    3 min read
  • How to Add Values to Dictionary in Python
    The task of adding values to a dictionary in Python involves inserting new key-value pairs or modifying existing ones. A dictionary stores data in key-value pairs, where each key must be unique. Adding values allows us to expand or update the dictionary's contents, enabling dynamic manipulation of d
    3 min read
  • Append a Value to a Dictionary Python
    The task of appending a value to a dictionary in Python involves adding new data to existing key-value pairs or introducing new key-value pairs into the dictionary. This operation is commonly used when modifying or expanding a dictionary with additional information. For example, consider the diction
    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
  • Python Program to print sum of all key value pairs in a Dictionary
    Given a dictionary arr consisting of N items, where key and value are both of integer type, the task is to find the sum of all key value pairs in the dictionary. Examples: Input: arr = {1: 10, 2: 20, 3: 30}Output: 11 22 33Explanation: Sum of key and value of the first item in the dictionary = 1 + 10
    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