DEV Community

Max
Max

Posted on

2

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 list.

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

# Output:
1, 2, 3, 4, 5, 6
Enter fullscreen mode Exit fullscreen mode

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 }

Enter fullscreen mode Exit fullscreen mode

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

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs