Last Updated on June 17, 2021 by Roshan Parihar
In this tutorial, learn how to add or update multiple items to Dictionary using Python. The short answer is: use the update()
function of Python to Add single or multiple elements to the Dictionary.
You can also use the update() to change or replace the old value of the existing keys of Dictionary. You may also like to read Add Element To Key In Dictionary Using Python.
If the key is new, it gets added to them with the new value. While, if the key is old but its value is new, it replaces the old value of the key with the new one.
Append or Change Single Element of Dictionary With Python
By using the update()
function of Python, you can add or change the single key value of Dictionary. To change the old value with the new one, you have to use the below-given method.
1 2 3 |
myDict = {'one': 'Sally', 'two': 13, 'three': 'Dingra', 'four': 'Lilop'}; myDict.update({'two':'Billy'}); print(myDict); |
Output
The above example contains the 4 elements including the keys and the associated values. The output contains the changed new value associated with the key ‘two’.
You can change only the single value using the above example. However, you can also add multiple keys and values in one go by using the below methods.
Add or Append Multiple Items To Dictionary in Python
In this example, you will find out how to add multiple keys in dictionary with its values. To add the new multiple items to the Dictionary, you have to use the update()
function and add all the items together in the function. These multiple items get appended to the end of the myDict
variable to add to dictionary in Python.
1 2 3 |
myDict = {'one': 'Sally', 'two': 13, 'three': 'Dingra', 'four': 'Lilop'}; myDict.update({'five':'Olive','six':'Nivo','seven':'Xavier'}); print(myDict); |
Output
The above example previously contains only the 4 items with the keys and associated elements values. However, after using the update function, the function append multiple values to dictionary and shows 7 elements that you can check in the above example.
Update Multiple Elements Using Python
In addition to adding the new multiple items, you can also update or change the multiple existing key associated elements using the same update()
function.
If the key value is new, it gets replaced by the old value. However, if the value is not new, the value associated with the key remains the same.
1 2 3 |
myDict = {'one': 'Sally', 'two': 13, 'three': 'Dingra', 'four': 'Lilop'}; myDict.update({'two':'Kelly','three':'Rancho','four':'Guru'}); print(myDict); |
Output
The above example showing the changed value for the keys ‘two’, ‘three’, and ‘four’. You can change or replace multiple numbers of items as per your requirement. You just have to provide the new value in the update function using Python.
FAQS on How to Update or Add Multiple Items to Dictionary in Python
1. How Do I Add Multiple Values to a Dictionary?
Answer: To add multiple values to a dictionary in Python, you can use the Python update()
function. You have to add multiple items with their keys and values to the update()
function.
When you execute the code, it checks for the keys if already present in the variable. If the key is not present in the variable, it adds the key with its value. If the key is present in the variable, it checks the value if different to replace with the specified item.
2. How Do I Add an Item to a Dictionary in Python?
Answer: To add an item to a dictionary in Python, you have to use the Python update()
function. You can specify single or multiple items with their keys and values in the Python update()
function.
You can follow the first and second example given above to add single or multiple items.
3. How Do You Add a Key to a Dictionary in Python?
Answer: To add a key to a dictionary, you have to use the Python update()
function and specify the key with its value. It first checks whether the specified key is already present in the variable or not. If the key is not matching in the variable, it appends the key with the specified value.
4. Can a Dictionary Have the Same Key in Python?
Answer: No, it cannot have the same key in Python. If you attempt to add the same key, it overrides the previous key with the new value. However, you can add multiple values to one key using the list in item values.
DOWNLOAD Free PYTHON CHEAT SHEET
You may also like to read
- Get dictionary value by key in Python
- Python Dictionary Create, Add, Delete, Looping With Examples
- Loop Through Python Dictionary Elements
I hope you like this post on how to update multiple items of Dictionary in Python. In the second example given above, Python Dictionary append or add multiple keys including its values.