How to Add Element in Dictionary Python?
Hey,
In this example, you will learn how to add element in dictionary python. I would like to show you python dictionary add element. you can see python dictionary add element if not exists. In this article, we will implement a 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:
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Python Create JSON File from List Example
- How to Check If a List is Empty or Not in Python?
- Python List Print All Elements Except Last Example
- How to Get Min Value from Python List?
- How to Convert List to Capitalize First Letter in Python?
- How to Get Max Value from Python List?
- How to Convert List to Uppercase in Python?
- Python Convert List into String with Commas Example
- How to Convert List into String in Python?
- How to Convert String into List in Python?
- How to Remove null Values from the List in Python?
- Python Remove Empty String from List Example
- How to Get First Element of List in Python?