Add a Key to a Dictionary in Python

Adding a new key to an existing dictionary in Python is straight forward. Just use the key reference syntax with the new key name and an assignment operator to set the value of the new key, like this:

car = {'brand': 'Tesla'}
car['model'] = 'Model 3'

print(car)
# {'brand': 'Tesla', 'model': 'Model 3'}

Add multiple keys

To add or update multiple keys at once you can use the dictionary's update() method.

car = {'brand': 'Tesla', 'model': 'Model 3'}
car.update({'model': 'Model S','top_speed_mph': 155.3})

print(car)
# {'brand': 'Tesla', 'model': 'Model S', 'top_speed_mph': 155.3}