W3docs

Add a new item to a dictionary in Python

To add a new item (key-value pair) to a dictionary in Python, you can use the square brackets [] or the update() method.

To add a new item (key-value pair) to a dictionary in Python, you can use the square brackets [] or the update() method. Here's an example using square brackets:

Assign a new item to a dictionary in Python

python— editable, runs on the server

This will output {'new_key': 'new_value'}.

Here's an example using the update() method:

Update a dictionary in Python and add a new item to it

python— editable, runs on the server

This will also output {'new_key': 'new_value'}.