Python – Add prefix to each key name in dictionary
Last Updated :
14 Mar, 2023
Given a dictionary, update its every key by adding a prefix to each key.
Input : test_dict = {‘Gfg’ : 6, ‘is’ : 7, ‘best’ : 9}, temp = “Pro” Output : {‘ProGfg’ : 6, ‘Prois’ : 7, ‘Probest’ : 9} Explanation : “Pro” prefix added to each key. Input : test_dict = {‘Gfg’ : 6, ‘is’ : 7, ‘best’ : 9}, temp = “a” Output : {‘aGfg’ : 6, ‘ais’ : 7, ‘abest’ : 9} Explanation : “a” prefix added to each key.
Method #1: Using dictionary comprehension
This is one of the methods by which this task can be performed. In this, we construct a new dictionary by performing the concatenation of prefixes with all keys.
Python3
test_dict = { 'Gfg' : 6 , 'is' : 7 , 'best' : 9 , 'for' : 8 , 'geeks' : 11 }
print ( "The original dictionary is : " + str (test_dict))
temp = "Pro"
res = {temp + str (key): val for key, val in test_dict.items()}
print ( "The extracted dictionary : " + str (res))
|
Output
The original dictionary is : {'Gfg': 6, 'is': 7, 'best': 9, 'for': 8, 'geeks': 11}
The extracted dictionary : {'ProGfg': 6, 'Prois': 7, 'Probest': 9, 'Profor': 8, 'Progeeks': 11}
Method #2 : Using f strings + dictionary comprehension
The combination of the above functionalities can be used to solve this problem. In this, we perform the task of concatenation using f strings. Works only in >=3.6 versions of Python.
Python3
test_dict = { 'Gfg' : 6 , 'is' : 7 , 'best' : 9 , 'for' : 8 , 'geeks' : 11 }
print ( "The original dictionary is : " + str (test_dict))
temp = "Pro"
res = {f "Pro{key}" : val for key, val in test_dict.items()}
print ( "The extracted dictionary : " + str (res))
|
Output
The original dictionary is : {'Gfg': 6, 'is': 7, 'best': 9, 'for': 8, 'geeks': 11}
The extracted dictionary : {'ProGfg': 6, 'Prois': 7, 'Probest': 9, 'Profor': 8, 'Progeeks': 11}
Method #3: Using dictionary comprehension with the zip() function
The zip() function creates tuples of the new keys and the values, and then converts the tuples back into a dictionary.
Step-by-Step Algorithm:
- Initialize the input dictionary.
- Initialize the prefix string.
- Using dictionary comprehension and zip(), create a list of tuples containing new keys with the prefix and old values.
- Convert the list of tuples to a dictionary.
- Print the extracted dictionary.
Python3
test_dict = { 'Gfg' : 6 , 'is' : 7 , 'best' : 9 , 'for' : 8 , 'geeks' : 11 }
print ( "The original dictionary is : " + str (test_dict))
temp = "Pro"
res = dict ( zip ([temp + key for key in test_dict.keys()], test_dict.values()))
print ( "The extracted dictionary : " + str (res))
|
Output
The original dictionary is : {'Gfg': 6, 'is': 7, 'best': 9, 'for': 8, 'geeks': 11}
The extracted dictionary : {'ProGfg': 6, 'Prois': 7, 'Probest': 9, 'Profor': 8, 'Progeeks': 11}
Complexity Analysis :
Time Complexity: O(n), where n is the number of elements in the dictionary. The dictionary comprehension operation is O(n), and the zip operation is also O(n).
Space Complexity: O(n), where n is the number of elements in the dictionary. The space required is for the dictionary res that stores the new keys and values.
Share your thoughts in the comments
Please Login to comment...