![]() |
VOOZH | about |
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.
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.
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 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.
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 #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.
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.
Method #5: Using dictionary() and zip()
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 * 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:
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.
Space complexity: O(n), where n is the number of items in the dictionary.