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, ‘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 dictionarytest_dict = {'"Geeks"' : 3, '"is" for' : 5, '"g"eeks' : 9}# printing original dictionaryprint("The original dictionary is : " + str(test_dict))# dictionary comprehension to make double quotes free# dictionaryres = {key.replace('"', ''):val for key, val in test_dict.items()} # printing resultprint("The dictionary after removal of double quotes : " + str(res)) |
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 comprehensionimport re# initializing dictionarytest_dict = {'"Geeks"' : 3, '"is" for' : 5, '"g"eeks' : 9}# printing original dictionaryprint("The original dictionary is : " + str(test_dict))# regex making replacement of double quotes with empty stringres = {re.sub(r'"', '', key): val for key, val in test_dict.items()} # printing resultprint("The dictionary after removal of double quotes : " + str(res)) |
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}





Please Login to comment...