Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Add prefix to each key name in dictionary

Python – Add prefix to each key name in dictionary

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




# Python3 code to demonstrate working of
# Add prefix to each key name in dictionary
# Using loop
 
# initializing dictionary
test_dict = {'Gfg' : 6, 'is' : 7, 'best' : 9, 'for' : 8, 'Lazyroar' : 11}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing prefix
temp = "Pro"
 
# + operator is used to perform task of concatenation
res = {temp + str(key): val for key, val in test_dict.items()}
 
# printing result
print("The extracted dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': 6, 'is': 7, 'best': 9, 'for': 8, 'Lazyroar': 11}
The extracted dictionary : {'ProGfg': 6, 'Prois': 7, 'Probest': 9, 'Profor': 8, 'ProLazyroar': 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




# Python3 code to demonstrate working of
# Add prefix to each key name in dictionary
# Using f strings + dictionary comprehension
 
# initializing dictionary
test_dict = {'Gfg' : 6, 'is' : 7, 'best' : 9, 'for' : 8, 'Lazyroar' : 11}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing prefix
temp = "Pro"
 
# dictionary comprehension is used to bind result
# f strings are used to bind prefix with key
res = {f"Pro{key}": val for key, val in test_dict.items()}
 
# printing result
print("The extracted dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': 6, 'is': 7, 'best': 9, 'for': 8, 'Lazyroar': 11}
The extracted dictionary : {'ProGfg': 6, 'Prois': 7, 'Probest': 9, 'Profor': 8, 'ProLazyroar': 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:

  1. Initialize the input dictionary.
  2. Initialize the prefix string.
  3. Using dictionary comprehension and zip(), create a list of tuples containing new keys with the prefix and old values.
  4. Convert the list of tuples to a dictionary.
  5. Print the extracted dictionary.

Python3




# initializing dictionary
test_dict = {'Gfg' : 6, 'is' : 7, 'best' : 9, 'for' : 8, 'Lazyroar' : 11}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing prefix
temp = "Pro"
 
# using dictionary comprehension with zip() to create tuples of new keys and values
res = dict(zip([temp+key for key in test_dict.keys()], test_dict.values()))
 
# printing result
print("The extracted dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': 6, 'is': 7, 'best': 9, 'for': 8, 'Lazyroar': 11}
The extracted dictionary : {'ProGfg': 6, 'Prois': 7, 'Probest': 9, 'Profor': 8, 'ProLazyroar': 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.

RELATED ARTICLES

Most Popular

Recent Comments