Python - Remove double quotes from dictionary keys
Last Updated :
05 May, 2023
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))
OutputThe 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))
OutputThe 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 key-value pairs in the input dictionary.
Auxiliary space: O(n), where n is the number of key-value pairs in the input dictionary.
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))
OutputThe 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 #4: 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))
OutputThe 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.
Method #5: Using dictionary() and zip()
- The original dictionary is created.
- The map() function is used with a lambda function to update the keys of the dictionary. The replace() function is used to remove the double quotes from the keys.
- The resulting dictionary is printed.
Python3
# Python3 code to demonstrate working of
# Remove double quotes from dictionary keys
# Using dictionary() and zip()
# 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 dictionary() and zip()
res = dict(zip((key.replace('"', '')
for key in test_dict.keys()), test_dict.values()))
# printing result
print("The dictionary after removal of double quotes : " + str(res))
OutputThe 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 * m) where n is the number of items in the input dictionary and m is the length of the string being replaced.
Space Complexity: O(n) where n is the number of items in the dictionary.
Method #6: Using the map() function to apply the string.replace() method to each key in the dictionary and remove double quotes
Step-by-step algorithm:
- Define the dictionary test_dict.
- Print the original dictionary.
- Use a dictionary comprehension to iterate over the items in the dictionary, replacing any double quotes in the key with an empty string and mapping the keys to a string data type.
- Create a new dictionary with the modified keys and the same values.
- Print the resulting dictionary after removal of double quotes.
Python3
#Initialize the dictionary with double quotes in its keys
test_dict = {'"Geeks"' : 3, '"is" for' : 5, '"g"eeks' : 9}
#Print the original dictionary
print("The original dictionary is : " + str(test_dict))
#Using dictionary comprehension, iterate over each key-value pair in the dictionary, and replace double quotes in keys with empty strings
res = {key.replace('"', ''): test_dict[key] for key in map(str, test_dict.keys())}
#Print the resulting dictionary with the modified keys
print("The dictionary after removal of double quotes : " + str(res))
OutputThe 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.
Space complexity: O(n), where n is the number of items in the dictionary.
Similar Reads
Remove Kth Key from Dictionary - Python We are given a dictionary we need to remove Kth key from the dictionary. For example, we are given a dictionary d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'} we need to remove the key2 so that the output should be {'key1': 'value1', 'key3': 'value3', 'key4': 'value4'}.
3 min read
Python Remove Key from Dictionary if Exists We are given a dictionary and a key and our task is to remove the key from the dictionary if it exists. For example, d = {"a": 10, "b": 20, "c": 30} and key to remove is "b" then output will be {"a": 10, "c": 30}.Using pop() pop() method allows us to remove a key from a dictionary while specifying a
2 min read
Remove Spaces from Dictionary Keys - Python Sometimes, the keys in a dictionary may contain spaces, which can create issues while accessing or processing the data. For example, consider the dictionary d = {'first name': 'Nikki', 'last name': 'Smith'}. We may want to remove spaces from the keys to standardize the dictionary, resulting in {'fir
3 min read
Remove Spaces from Dictionary Keys - Python Sometimes, the keys in a dictionary may contain spaces, which can create issues while accessing or processing the data. For example, consider the dictionary d = {'first name': 'Nikki', 'last name': 'Smith'}. We may want to remove spaces from the keys to standardize the dictionary, resulting in {'fir
3 min read
Python - Remove Item from Dictionary There are situations where we might want to remove a specific key-value pair from a dictionary. For example, consider the dictionary d = {'x': 10, 'y': 20, 'z': 30}. If we need to remove the key 'y', there are multiple ways to achieve this. Let's discuss several methods to remove an item from a dict
3 min read
Python - Remove Multiple Keys from Dictionary We are given a dictionary and our task is to remove multiple keys from the dictionary. For example, consider a dictionary d = {'a': 1, 'b': 2, 'c': 3, 'd': 4} where we want to remove the keys 'b' and 'd', then the output will be {'a': 1, 'c': 3}. Let's explore different methods to remove multiple ke
3 min read