Add a Key to a Dictionary in Python

1 min read

Add one key

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 S'}

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}


Author picture
Rhett Trickett

Founder at Able

@rhett RhettTrickett rhetttrickett
Python

Join the discussion

Able is a developer community where people build their coding knowledge and careers.

Join with GitHub Join with Twitter