Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Remove double quotes from dictionary keys

Improve Article
Save Article
  • Last Updated : 14 Mar, 2023
Improve Article
Save Article

Given dictionary with string keys, remove double quotes from it.

Input : test_dict = {‘”Geeks”‘ : 3, ‘”g”eeks’ : 9} 
Output : {‘Geeks’: 3, ‘geeks’: 9} 
Explanation : Double quotes removed from keys.

Input : test_dict = {‘”Geeks”‘ : 3} 
Output : {‘Geeks’: 3} 
Explanation : Double quotes removed from keys. 

Method #1 :  Using dictionary comprehension + replace()

The combination of above functionalities can be used to solve this problem. In this, we perform removal of double quotes using replace() with empty string. The dictionary comprehension is used for remaking dictionary.

Python3




# Python3 code to demonstrate working of
# Remove double quotes from dictionary keys
# Using dictionary comprehension + replace()
 
# initializing dictionary
test_dict = {'"Geeks"' : 3, '"is" for' : 5, '"g"eeks' : 9}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# dictionary comprehension to make double quotes free
# dictionary
res = {key.replace('"', ''):val for key, val in test_dict.items()}
     
# printing result
print("The dictionary after removal of double quotes : " + str(res))


Output

The original dictionary is : {'"Geeks"': 3, '"is" for': 5, '"g"eeks': 9}
The dictionary after removal of double quotes : {'Geeks': 3, 'is for': 5, 'geeks': 9}

Method #2 : Using re.sub() + dictionary comprehension

The combination of above functions is also an alternative to solve this task. In this, we employ regex to solve the problem. 

Python3




# Python3 code to demonstrate working of
# Remove double quotes from dictionary keys
# Using re.sub() + dictionary comprehension
import re
 
# initializing dictionary
test_dict = {'"Geeks"' : 3, '"is" for' : 5, '"g"eeks' : 9}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# regex making replacement of double quotes with empty string
res = {re.sub(r'"', '', key): val for key, val in test_dict.items()}
     
# printing result
print("The dictionary after removal of double quotes : " + str(res))


Output

The original dictionary is : {'"Geeks"': 3, '"is" for': 5, '"g"eeks': 9}
The dictionary after removal of double quotes : {'Geeks': 3, 'is for': 5, 'geeks': 9}

Method #3: Using a for loop to iterate over the dictionary items and update the keys

This Python code removes double quotes from dictionary keys using a for loop. It initializes a dictionary with keys containing double quotes, replaces them with single quotes using a for loop, and creates a new dictionary with updated keys. The original and updated dictionaries are printed using the print function.

Python3




# Python3 code to demonstrate working of
# Remove double quotes from dictionary keys
# Using a for loop
 
# initializing dictionary
test_dict = {'"Geeks"' : 3, '"is" for' : 5, '"g"eeks' : 9}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# iterate over the dictionary items and update the keys
new_dict = {}
for key, val in test_dict.items():
    new_key = key.replace('"', '')
    new_dict[new_key] = val
     
# printing result
print("The dictionary after removal of double quotes : " + str(new_dict))


Output

The original dictionary is : {'"Geeks"': 3, '"is" for': 5, '"g"eeks': 9}
The dictionary after removal of double quotes : {'Geeks': 3, 'is for': 5, 'geeks': 9}

Time complexity: O(n), where n is the number of items in the dictionary.
Auxiliary space: O(n), as a new dictionary new_dict is created and populated with each key-value pair in the original dictionary.

Method #7: Using map() function with lambda function to update the keys

This program removes the double quotes from the keys of a dictionary. It uses the map() function with a lambda function to update the keys, and creates a new dictionary with the updated keys.

Python3




# Python3 code to demonstrate working of
# Remove double quotes from dictionary keys
# Using map() function with lambda function
 
# initializing dictionary
test_dict = {'"Geeks"' : 3, '"is" for' : 5, '"g"eeks' : 9}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# updating the keys using map() function with lambda
res = dict(map(lambda key_val: (key_val[0].replace('"', ''), key_val[1]), test_dict.items()))
 
# printing result
print("The dictionary after removal of double quotes : " + str(res))


Output

The original dictionary is : {'"Geeks"': 3, '"is" for': 5, '"g"eeks': 9}
The dictionary after removal of double quotes : {'Geeks': 3, 'is for': 5, 'geeks': 9}

Time Complexity: O(n*logn), where n is the number of items in the input dictionary. The map() function with lambda expression takes O(nlogn) time complexity.
Auxiliary Space: O(n), where n is the number of items in the input dictionary. The space complexity is dominated by the new dictionary created as output.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!