W3docs

How can I add new keys to a dictionary?

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

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

Here's an example:

Add a new key-value pair to a dictionary in Python using the update method

python— editable, runs on the server
Info

Watch a video course: Python - The Practical Guide

You can also use the update() method to add multiple key-value pairs at once:

Add multiple new key-value pairs to a dictionary in Python using the update method

python— editable, runs on the server