Python dictionary append

Python dictionary is a collection key value pair, unlike like python list, values in dictionary are stored and accessed using key, where in list we will use index.

In list to append a value, we can use a inbuild list method append to add a value to li…


This content originally appeared on DEV Community and was authored by Max

Python dictionary is a collection key value pair, unlike like python list, values in dictionary are stored and accessed using key, where in list we will use index.

In list to append a value, we can use a inbuild list method append to add a value to list.

l = [1,2,3,4,5]
l.append(6)
print(l)

# Output:
1, 2, 3, 4, 5, 6

Append a value to dictionary

To add a new value to dictionary, simply use the assignment operator, if the key value is not in dict, python will add a new entry. If the key is already exist in dict then it will update the value

d = { "a": 1, "b": 2 }
d["c"] = 3
print(d)

# Output
{ "a": 1, "b": 2, "c": 3 }

d["c"] = 20
print(d)

# Output
{ "a": 1, "b": 20, "c": 3 }

Explore Other Dev.to Articles

Python One Line While Loop
Read and write csv file in Python
Python Install Jupyter Notebook
Python Create Virtual Environment
Read and Write JSON in Python Requests from API


This content originally appeared on DEV Community and was authored by Max


Print Share Comment Cite Upload Translate Updates
APA

Max | Sciencx (2023-03-25T16:37:53+00:00) Python dictionary append. Retrieved from https://www.scien.cx/2023/03/25/python-dictionary-append/

MLA
" » Python dictionary append." Max | Sciencx - Saturday March 25, 2023, https://www.scien.cx/2023/03/25/python-dictionary-append/
HARVARD
Max | Sciencx Saturday March 25, 2023 » Python dictionary append., viewed ,<https://www.scien.cx/2023/03/25/python-dictionary-append/>
VANCOUVER
Max | Sciencx - » Python dictionary append. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/25/python-dictionary-append/
CHICAGO
" » Python dictionary append." Max | Sciencx - Accessed . https://www.scien.cx/2023/03/25/python-dictionary-append/
IEEE
" » Python dictionary append." Max | Sciencx [Online]. Available: https://www.scien.cx/2023/03/25/python-dictionary-append/. [Accessed: ]
rf:citation
» Python dictionary append | Max | Sciencx | https://www.scien.cx/2023/03/25/python-dictionary-append/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.