Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Remove double quotes from dictionary keys

Python – Remove double quotes from dictionary keys

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

Input : test_dict = {‘”Geeks”‘ : 3, ‘”g”eeks’ : 9} 
Output : {‘Geeks’: 3, ‘Lazyroar’: 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, 'Lazyroar': 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, 'Lazyroar': 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))


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, 'Lazyroar': 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))


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, 'Lazyroar': 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))


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, 'Lazyroar': 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:

  1. Define the dictionary test_dict.
  2. Print the original dictionary.
  3. 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.
  4. Create a new dictionary with the modified keys and the same values.
  5. 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))


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, 'Lazyroar': 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.

RELATED ARTICLES

Most Popular

Recent Comments