
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 like this ?
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 operator
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 D = {"name": "Tutorialspoint", "location": "Hyderabad"} # Intializing an empty dictionary new_dict = {} # Looping over each key in dictionary # and adding prefix to them for k, v in D.items(): new_dict[f"Company_{k}"] = v # Printing the new dictionary print(new_dict)
Output
{'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 D = {"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 D.items()} # Printing the new dictionary print(new_dict)
Output
{'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 D = {"name": "Tutorialspoint", "location": "Hyderabad"} #Adding prefix to dictianary keys new_dict = dict(map(lambda item: ("Comapny_" + item[0], item[1]), D.items())) # Printing the new dictionary print(new_dict)
Output
{'Comapny_name': 'Tutorialspoint', 'Comapny_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 D = {"name": "Tutorialspoint", "location": "Hyderabad"} #Defining a function def new_dict(D, prefix): return {prefix + k: v for k, v in D.items()} # Calling function and # Printing the new dictionary print(new_dict(D,"Company_"))
Output
{'Company_name': 'Tutorialspoint', 'Company_location': 'Hyderabad'}
Conclusion
In this article, we have seen multiple approaches for adding a prefix to each key name in a Python dictionary. The dictionary comprehension and the map() function are a concise way that allows us to achieve this task using a single line of statement, whereas for loop performs the taks step by step.