Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Adding a prefix to each key name in a Python dictionary
In this article, we will understand how to add a prefix to each key name in a Python dictionary. Dictionaries in Python are collections of unordered items stored in the form of key-value pairs inside curly braces ?
Dictionary = {key: value, ...}
Our goal is to add a prefix (i.e., a word or character added at the beginning of another word) to each key name of the dictionary in Python.
Suppose a key in a dictionary is name, on adding the prefix company_ to it, the key becomes company_name.
For example ?
# Input dictionary:
dictionary = {'name': 'Tutorialspoint', 'location': 'Hyderabad'}
# Prefix to add
prefix = 'Company_'
# Output dictionary
new_dictionary = {'Company_name': 'Tutorialspoint', 'Company_location': 'Hyderabad'}
Adding Prefix to Key Name in Python Dictionary
We can add a prefix to keys name of a dictionary in Python using the following approaches ?
- Using a For loop
- Using Dictionary Comprehension
- Using map() function and "+" operator
- Using a Function and Dictionary comprehension
Let us go through this all one by one ?
Using a For Loop
The for loop in Python is used to traverse a sequence of items. Using this, we can add a prefix to the dictionary keys by looping through them and their associated values. Then, we can insert the updated key-value pairs to an empty dictionary ?
Example
The following program prints the dictionary key with a prefix using a for loop in Python ?
# Initializing a dictionary
data = {"name": "Tutorialspoint", "location": "Hyderabad"}
# Initializing an empty dictionary
new_dict = {}
# Looping over each key in dictionary
# and adding prefix to them
for k, v in data.items():
new_dict[f"Company_{k}"] = v
# Printing the new dictionary
print(new_dict)
{'Company_name': 'Tutorialspoint', 'Company_location': 'Hyderabad'}
Using Dictionary Comprehension
The dictionary comprehension (or dict comprehension) is used to create a dictionary in Python from key-value pairs of a provided iterable. We can perform an operation on each key-value pair of the iterable and store the resulting values as a dictionary.
To add a prefix to a dictionary key name, we need to pass it as an iterable to a dictionary comprehension. It will create a new dictionary using the original keys and values, with the prefix added to the key names ?
Example
The following program prints the dictionary key with a prefix using dictionary comprehension in Python ?
# Initializing a dictionary
data = {"name": "Tutorialspoint", "location": "Hyderabad"}
# Looping over each key in dictionary
# and adding a prefix to them
new_dict = {f"Company_{k}": v for k, v in data.items()}
# Printing the new dictionary
print(new_dict)
{'Company_name': 'Tutorialspoint', 'Company_location': 'Hyderabad'}
Using map() Function with "+" Operator
The map() function in Python accepts two arguments, a function and an iterable. It returns a new iterable object by applying the specified function to each item of a given iterable.
To add a prefix to key names of a dictionary, we need to pass the dictionary as an iterable and a function to the map() function. This function concatenates the prefix with key name in a dictionary using the '+' operator ?
Example
The following program prints the dictionary key with a prefix using a map() function and "+" operator in Python ?
# Initializing a dictionary
data = {"name": "Tutorialspoint", "location": "Hyderabad"}
# Adding prefix to dictionary keys
new_dict = dict(map(lambda item: ("Company_" + item[0], item[1]), data.items()))
# Printing the new dictionary
print(new_dict)
{'Company_name': 'Tutorialspoint', 'Company_location': 'Hyderabad'}
Using a Function and Dictionary Comprehension
We can add a prefix to dictionary keys in Python using a function. This function takes a dictionary and its keys as arguments and returns a new dictionary having prefix with each key names using dictionary comprehension and concatenation ("+" operator) ?
Example
The following program prints the dictionary key with a prefix using a function and dictionary comprehension in Python ?
# Initializing a dictionary
data = {"name": "Tutorialspoint", "location": "Hyderabad"}
# Defining a function
def add_prefix(dictionary, prefix):
return {prefix + k: v for k, v in dictionary.items()}
# Calling function and
# Printing the new dictionary
print(add_prefix(data, "Company_"))
{'Company_name': 'Tutorialspoint', 'Company_location': 'Hyderabad'}
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| For Loop | High | Slower | Beginners |
| Dict Comprehension | High | Fast | General use |
| map() Function | Medium | Fast | Functional programming |
| Custom Function | High | Fast | Reusable code |
Conclusion
Dictionary comprehension is the most Pythonic and efficient approach for adding prefixes to dictionary keys. Use custom functions when you need reusability across your codebase. For loops offer clarity for beginners but are less efficient.
