Assign the same value to multiple dictionary keys at once in Python

avatar

Borislav Hadzhiev

Last updated: Sep 17, 2022

banner

Photo from Unsplash

Assign the same value to multiple dictionary keys at once in Python #

To assign the same value to multiple dictionary keys at once:

  1. Use the dict.fromkeys() method to get a dictionary with the updated key-value pairs.
  2. Use the dict.update() method to update the original dictionary.
  3. The specified keys of the dictionary will get set to the supplied value.
main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, 'language': 'Python', } keys_to_update = ['name', 'language', 'id'] my_dict.update(dict.fromkeys(keys_to_update, None)) # 👇️ {'name': None, 'site': 'bobbyhadz.com', 'id': None, 'language': None} print(my_dict)

We used the dict.fromkeys() method to create a new dictionary containing the updated key-value pairs.

The dict.fromkeys method takes an iterable and a value and creates a new dictionary with keys from the iterable and values set to the provided value.

main.py
keys_to_update = ['name', 'language', 'id'] result = dict.fromkeys(keys_to_update, 'default') # 👇️ {'name': 'default', 'language': 'default', 'id': 'default'} print(result)

The last step is to use the dict.update() method to assign the same value to the specified dictionary keys at once.

main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, 'language': 'Python', } keys_to_update = ['name', 'language', 'id'] my_dict.update(dict.fromkeys(keys_to_update, None)) # 👇️ {'name': None, 'site': 'bobbyhadz.com', 'id': None, 'language': None} print(my_dict)

We used a default value of None in the example, but you can use any other value.

The dict.update method updates the dictionary with the key-value pairs from the provided value.

main.py
my_dict = {'name': 'Alice'} my_dict.update({'name': 'bobbyhadz', 'age': 30}) print(my_dict) # 👉️ {'name': 'bobbyhadz', 'age': 30}

The method overrides the dictionary's existing keys and returns None.

The dict.update() method can either be called with another dictionary or an iterable of key-value pairs (e.g. a list of tuples with 2 elements each).

Alternatively, you can use the dictionary unpacking ** operator.

Assign the same value to multiple dictionary keys at once using dictionary unpacking #

To assign the same value to multiple dictionary keys at once:

  1. Use the dict.fromkeys() method to get a dictionary with the updated key-value pairs.
  2. Use dictionary unpacking to unpack the two dictionaries.
  3. The specified keys in the original dictionary will get set to the supplied value.
main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, 'language': 'Python', } keys_to_update = ['name', 'language', 'id'] my_dict = { **my_dict, **dict.fromkeys(keys_to_update, 0) } # 👇️ {'name': 0, 'site': 'bobbyhadz.com', 'id': 0, 'language': 0} print(my_dict)

We used the dictionary unpacking ** operator to unpack the key-value pairs of the dictionaries into a third dictionary.

The key-value pairs of the second dictionary override the existing key-value pairs, setting the specified keys to the supplied value.

If you're looking for a more readable and beginner-friendly solution, use a for loop.

Assign the same value to multiple dictionary keys using a for loop #

To assign the same value to multiple dictionary keys:

  1. Use a for loop to iterate over the collection of keys.
  2. Assign the value to each key.
  3. After the last iteration, the specified keys will be set to the supplied value.
main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, 'language': 'Python', } keys_to_update = ['name', 'language', 'id'] for key in keys_to_update: my_dict[key] = None # 👇️ {'name': None, 'site': 'bobbyhadz.com', 'id': None, 'language': None} print(my_dict)

We used a for loop to iterate over the collection of keys and set the value of each key to None.

The for loop approach is more manual but is also easier to read.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.