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

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

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