Add Element in Dictionary Python Tutorial Example

24-Jan-2023

.

Admin

Add Element in Dictionary Python Tutorial Example

Hi Dev,

This article is focused on add element in dictionary python tutorial example. if you want to see example of python add new element to dict then you are a right place. you can understand a concept of python add new element to dictionary. you can see how to add an element in a dictionary python. Here, Creating a basic example of how to add new element in dictionary python.

There are several ways to add a new element to the 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:

Example 1:


main.py

user = {

"ID": 1,

"name": "Piyush Kamani",

"email": "piyush@gmail.com"

}

# Adding new "bithdate" to dictionary

user["bithdate"] = "1998/07/01"

print(user)

Output:

{

'ID': 1,

'bithdate': '1998/07/01',

'email': 'piyush@gmail.com',

'name': 'Piyush Kamani'

}

Example 2:

main.py

user = {

"ID": 1,

"name": "Piyush Kamani",

"email": "piyush@gmail.com"

}

# Adding new "bithdate" to dictionary

user.update({"bithdate": "1998/07/01"})

print(user)

Output:

{

'ID': 1,

'bithdate': '1998/07/01',

'email': 'piyush@gmail.com',

'name': 'Piyush Kamani'

}

#Python