ItSolutionStuff.com

How to Add Item in Dictionary Python?

By Hardik Savani October 30, 2023
Python

Hey Artisan,

This post is focused on how to add item in dictionary python. Here you will learn python dictionary add item. you can understand a concept of python dictionary add item if not exists. step by step explain how to add new item in dictionary python.

There are several ways to add a new item to a dictionary in python. I will give you simple two examples using adding index and value and update() method. so let's see the below examples:

You can use these examples with python3 (Python 3) version.

Example 1:

main.py

user = {
  "ID": 1,
  "name": "Hardik Savani",
  "email": "hardik@gmail.com"
}
  
# Adding new "bithdate" to dictionary
user["bithdate"] = "1994/07/01"
  
print(user)

Output:

{
 'ID': 1,
 'bithdate': '1994/07/01',
 'email': 'hardik@gmail.com',
 'name': 'Hardik Savani'
}

Example 2:

main.py

user = {
  "ID": 1,
  "name": "Hardik Savani",
  "email": "hardik@gmail.com"
}
    
# Adding new "bithdate" to dictionary
user.update({"bithdate": "1994/07/01"})
    
print(user)

Output:

{
 'ID': 1,
 'bithdate': '1994/07/01',
 'email': 'hardik@gmail.com',
 'name': 'Hardik Savani'
}

I hope it can help you...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Check If a List is Empty or Not in Python?

Read Now →

Python Read Text File into List Example

Read Now →

How to Count Number of Elements in a List in Python?

Read Now →

How to Find Average of List in Python?

Read Now →

How to Get Min Value from Python List?

Read Now →

How to Convert List to Capitalize First Letter in Python?

Read Now →

How to Get Max Value from Python List?

Read Now →

Python Convert List into String with Commas Example

Read Now →

Python Split String into List of Characters Example

Read Now →

How to Convert String into List in Python?

Read Now →

How to Remove Duplicate Values from List in Python?

Read Now →

How to Get Unique Elements from List in Python?

Read Now →