Open In App

Python | Remove spaces from dictionary keys

Improve
Improve
Like Article
Like
Save
Share
Report

In Python, dictionary is a collection which is unordered, changeable and indexed. Dictionaries are written with curly brackets, and they have keys and values. It is used to hash a particular key. Let’s see how to remove spaces from dictionary keys in Python. 

Method #1: Using translate() function here we visit each key one by one and remove space with the none. Here translate function takes parameter 32, none where 32 is ASCII value of space ‘ ‘ and replaces it with none. 

Python3




# Python program to remove space from keys
 
# creating a dictionary of type string
 
Product_list = {'P 01' : 'DBMS', 'P 02' : 'OS',
                'P 0 3 ': 'Soft Computing'}
 
# removing spaces from keys
# storing them in same dictionary
Product_list = { x.translate({32:None}) : y
                 for x, y in Product_list.items()}
 
# printing new dictionary
print (" New dictionary : ", Product_list)


Output:

New dictionary :  {'P01': 'DBMS', 'P03': 'Soft Computing', 'P02': 'OS'}

Time Complexity: The time complexity of this program is O(n), where n is the number of key-value pairs in the dictionary

Auxiliary Space: The space complexity of this program is also O(n). The original dictionary is stored in memory, along with the new dictionary that is created using the dictionary comprehension.

Method #2: Using replace() function. In this method, we visit each key in dictionary one by one and replace all spaces in key with no space. This function takes as argument space and second non-space. 

Python3




# Python program to remove space from keys
 
# creating a dictionary of type string
 
Product_list = {'P 01' : 'DBMS', 'P 02' : 'OS',
                'P 0 3 ': 'Soft Computing'};
 
# removing spaces from keys
# storing them in same dictionary
Product_list = {x.replace(' ', ''): v
     for x, v in Product_list.items()}
 
# printing new dictionary
print (" New dictionary : ", Product_list)


Output:

New dictionary :  {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}

The time complexity of the above program is O(N) where N is the number of key-value pairs in the dictionary. 

The auxiliary space used by the program is also O(N), as it creates a new dictionary with the modified keys, which has the same number of key-value pairs as the original dictionary. 

Method #3: Using re module stands for “regular expression” 

Here is an example of using the re module to remove spaces from dictionary keys:

Python3




import re
 
# creating a dictionary of type string
Product_list = {'P 01' : 'DBMS', 'P 02' : 'OS', 'P 0 3 ': 'Soft Computing'}
 
# removing spaces from keys
Product_list = {re.sub('\s+', '', key): value for key, value in Product_list.items()}
 
# printing new dictionary
print("New dictionary:", Product_list)
#This code is contributed by Edula Vinay Kumar Reddy


Output

New dictionary: {'P01': 'DBMS', 'P02': 'OS', 'P03': 'Soft Computing'}

Time complexity: O(n), where n is the number of key-value pairs in the original dictionary.
Auxiliary space: O(n)

Method 4: Using a dictionary comprehension with the strip() method

This program creates a dictionary with some key-value pairs, and then uses a dictionary comprehension to create a new dictionary with the same values, but with leading and trailing spaces removed from the keys. Finally, it prints the new dictionary.

Python3




Product_list = {'P 01' : 'DBMS', 'P 02' : 'OS',
                'P 0 3 ': 'Soft Computing'}
 
Product_list = {key.strip(): value for key, value in Product_list.items()}
 
print("New dictionary:", Product_list)


Output

New dictionary: {'P 01': 'DBMS', 'P 02': 'OS', 'P 0 3': 'Soft Computing'}

Time complexity: O(N), where N is the number of items in the dictionary.
Auxiliary space: O(N), because we are creating a new dictionary with the same number of items as the original dictionary.

Method 5 : using the map() function along with a lambda function

step-by-step approach :

  1. Define a dictionary Product_list with three key-value pairs.
  2. Call the map() function with a lambda function as its argument and Product_list.items() as its iterable.
  3. The lambda function takes in a tuple x (representing a key-value pair) and returns a tuple where the first element is the modified key with spaces removed using the replace() method and the second element is the value.
  4. The map() function applies the lambda function to each key-value pair in Product_list and returns an iterable of modified key-value pairs.
  5. Convert the iterable of modified key-value pairs into a dictionary using the dict() function.
  6. Assign the newly created dictionary to the variable Product_list.
  7. Print the modified dictionary using the print() function.

Python3




# Python program to remove space from keys
 
# creating a dictionary of type string
Product_list = {'P 01' : 'DBMS', 'P 02' : 'OS', 'P 0 3 ': 'Soft Computing'}
 
# removing spaces from keys using map() and lambda
Product_list = dict(map(lambda x: (x[0].replace(' ', ''), x[1]), Product_list.items()))
 
# printing new dictionary
print("New dictionary: ", Product_list)


Output

New dictionary:  {'P01': 'DBMS', 'P02': 'OS', 'P03': 'Soft Computing'}

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.

Auxiliary space: O(n), as a new dictionary is created to store the modified key-value pairs.



Last Updated : 28 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads