Remove spaces from dictionary keys in Python

avatar

Borislav Hadzhiev

Last updated: Aug 20, 2022

banner

Photo from Unsplash

Remove spaces from dictionary keys in Python #

To remove the spaces from the keys in a dictionary:

  1. Use a dict comprehension to iterate over the dictionary.
  2. Use the str.replace() method to remove the spaces from each key.
  3. Return the new keys and the values from the dict comprehension.
main.py
my_dict = { 'o n e': 1, 't w o': 2, 't h r e e': 3 } # ✅ Remove spaces from dictionary keys (using dict comprehension) new_dict = { key.replace(' ', ''): value for key, value in my_dict.items() } # 👇️ {'one': 1, 'two': 2, 'three': 3} print(new_dict) print(new_dict['one']) # 👉️ 1 print(new_dict['two']) # 👉️ 2 # ------------------------------------------------------- # ✅ Remove spaces from dictionary keys (using for loop) new_dict = {} for key, value in my_dict.items(): new_dict[key.replace(' ', '')] = value # 👇️ {'one': 1, 'two': 2, 'three': 3} print(new_dict) print(new_dict['one']) # 👉️ 1 print(new_dict['two']) # 👉️ 2

The first example uses a dict comprehension to remove the spaces from the keys in a dictionary.

Dict comprehensions are very similar to list comprehensions.

They perform some operation for every key-value pair in the dictionary or select a subset of key-value pairs that meet a condition.
main.py
my_dict = { 'o n e': 1, 't w o': 2, 't h r e e': 3 } new_dict = { key.replace(' ', ''): value for key, value in my_dict.items() } # 👇️ {'one': 1, 'two': 2, 'three': 3} print(new_dict)

The dict.items method returns a new view of the dictionary's items ((key, value) pairs).

main.py
my_dict = { 'o n e': 1, 't w o': 2, 't h r e e': 3 } # 👇️ dict_items([('o n e', 1), ('t w o', 2), ('t h r e e', 3)]) print(my_dict.items())

On each iteration, we use the str.replace() method to remove the spaces from the current key and return the key-value pair.

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)
We remove the spaces from each key by replacing each occurrence of a space with an empty string.

Alternatively, you can use a simple for loop.

Remove spaces from dictionary keys using for loop #

To remove the spaces from the keys in a dictionary:

  1. Use a for loop to iterate over the dictionary.
  2. Use the str.replace() method to remove the spaces from each key.
  3. Add each key-value pair to a new dictionary.
main.py
my_dict = { 'o n e': 1, 't w o': 2, 't h r e e': 3 } new_dict = {} for key, value in my_dict.items(): new_dict[key.replace(' ', '')] = value # 👇️ {'one': 1, 'two': 2, 'three': 3} print(new_dict) print(new_dict['one']) # 👉️ 1 print(new_dict['two']) # 👉️ 2

We declared a new_dict variable that will store the key-value pairs where the keys don't contain spaces.

On each iteration, we use the str.replace() method to remove the spaces from the current key and assign the key-value pair to the new dictionary.

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.