Sometimes, while working with dictionaries, we might have utility problem in which we need to perform elementary operation among the common keys of dictionaries. This can be extended to any operation to be performed. Let’s discuss string concatenation of like key values and ways to solve it in this article.
Method #1 : Using dictionary comprehension + keys()
The combination of above two can be used to perform this particular task. This is just a shorthand to the longer method of loops and can be used to perform this task in one line.
# Python3 code to demonstrate working of # Concatenate Dictionary string values # Using dictionary comprehension + keys() # Initialize dictionaries test_dict1 = {'gfg' : 'a', 'is' : 'b', 'best' : 'c'} test_dict2 = {'gfg' : 'd', 'is' : 'e', 'best' : 'f'} # printing original dictionaries print("The original dictionary 1 : " + str(test_dict1)) print("The original dictionary 2 : " + str(test_dict2)) # Using dictionary comprehension + keys() # Concatenate Dictionary string values res = {key: test_dict1[key] + test_dict2.get(key, '') for key in test_dict1.keys()} # printing result print("The string concatenation of dictionary is : " + str(res)) |
The original dictionary 1 : {'gfg': 'a', 'is': 'b', 'best': 'c'}
The original dictionary 2 : {'gfg': 'd', 'is': 'e', 'best': 'f'}
The string concatenation of dictionary is : {'gfg': 'ad', 'is': 'be', 'best': 'cf'}
Method #2 : Using Counter() + “+” operator
The combination of above method can be used to perform this particular task. In this, the Counter function converts the dictionary in the form in which the plus operator can perform the task of concatenation.
# Python3 code to demonstrate working of # Concatenate Dictionary string values # Using Counter() + "+" operator from collections import Counter # Initialize dictionaries test_dict1 = {'gfg' : 'a', 'is' : 'b', 'best' : 'c'} test_dict2 = {'gfg' : 'd', 'is' : 'e', 'best' : 'f'} # printing original dictionaries print("The original dictionary 1 : " + str(test_dict1)) print("The original dictionary 2 : " + str(test_dict2)) # Using Counter() + "+" operator # Concatenate Dictionary string values temp1 = Counter(test_dict1) temp2 = Counter(test_dict2) res = Counter({key : temp1[key] + temp2[key] for key in temp1}) # printing result print("The string concatenation of dictionary is : " + str(dict(res))) |
The original dictionary 1 : {'gfg': 'a', 'is': 'b', 'best': 'c'}
The original dictionary 2 : {'gfg': 'd', 'is': 'e', 'best': 'f'}
The string concatenation of dictionary is : {'gfg': 'ad', 'is': 'be', 'best': 'cf'}
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python - Concatenate String values in Dictionary List
- Python - Concatenate Ranged Values in String list
- Python | Concatenate dictionary value lists
- Python - Concatenate Tuple to Dictionary Key
- Python program to concatenate two Integer values into one
- Python - Concatenate values with same keys in a list of dictionaries
- Python - Filter dictionary values in heterogenous dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python program to update a dictionary with the values from a dictionary list
- How to Concatenate Column Values in Pandas DataFrame?
- Python - Extract Unique values dictionary values
- Python - Remove duplicate values across Dictionary Values
- Python - Test for Even values dictionary values lists
- Python | Pandas Series.str.cat() to concatenate string
- Python | Ways to concatenate boolean to string
- Python | Concatenate N consecutive elements in String list
- Python - Concatenate string rows in Matrix
- Python - Concatenate Random characters in String List
- Python program to Concatenate Kth index words of String
- Python | Convert string dictionary to 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.

