Given a dictionary, update its each key by adding 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 in which this task can be performed. In this we construct new dictionary by performing concatenation of prefix 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, 'geeks' : 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)) |
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 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, 'geeks' : 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)) |
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}
Recommended Posts:
- Python | Prefix key match in dictionary
- Python - Extract Key's Value, if Key Present in List and Dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Add a key:value pair to dictionary in Python
- Python - Add item after given Key in dictionary
- Python | Sum values for each key in nested dictionary
- Python | Perform operation on each key dictionary
- Python | K modulo on each Dictionary Key
- Python IMDbPY – Get each episode name of each season of the series
- Python | Print the initials of a name with last name in full
- Python IMDbPY – Getting Person name from searched name
- GUI application to search a country name from a given state or city name using Python
- Python - Convert List to key-value list by prefix grouping
- Python - Extract target key from other key values
- Python - Filter key's value from other key
- Python | Extract key-value of dictionary in variables
- How to use a List as a key of a Dictionary in Python 3?
- Python | Ways to remove a key from dictionary
- Python | Check whether given key already exists in a dictionary
- Python | Accessing Key-value in Dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

