Sometimes, while working with Dictionaries, we can have a task in which we need to perform the conversion of converting dictionary to string, which is concatenated key-value pair. This can have application in domains in which we require to reduce storage space or require strings as target data. Lets discuss certain ways in which this task can be performed.
Method #1 : Using join() + items()
The combination of above functions can be used to solve this problem. In this, we need to perform the task of concatenation using join() and extraction of dictionary items is done using items().
# Python3 code to demonstrate working of # Convert Dictionary to Concatenated String # Using join() + items() # initializing dictionary test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # Convert Dictionary to Concatenated String # Using join() + items() res = ''.join(key + str(val) for key, val in test_dict.items()) # printing result print("The dictionary after concatenation is : " + str(res)) |
The original dictionary is : {'gfg': 1, 'best': 3, 'is': 2}
The dictionary after concatenation is : gfg1best3is2
Method #2 : Using reduce() + lambda
The combination of above functions can be used to perform this task. In this, we perform the task of concatenation using combination of reduce() and lambda.
# Python3 code to demonstrate working of # Convert Dictionary to Concatenated String # Using reduce() + lambda from functools import reduce # initializing dictionary test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # Convert Dictionary to Concatenated String # Using reduce() + lambda res = reduce(lambda key, val : key + str(val[0]) + str(val[1]), test_dict.items(), '') # printing result print("The dictionary after concatenation is : " + str(res)) |
The original dictionary is : {'gfg': 1, 'best': 3, 'is': 2}
The dictionary after concatenation is : gfg1best3is2
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 | Convert string dictionary to dictionary
- Concatenated string with uncommon characters in Python
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python - Convert Dictionary Value list to Dictionary List
- Python | Convert dictionary object into string
- Python | Convert byteString key:value pair of dictionary to String
- Python | Convert key-value pair comma separated string into dictionary
- Python - Convert key-value String to dictionary
- Python - Convert String List to Key-Value List dictionary
- Python program to split the string and convert it to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Regular Dictionary vs Ordered Dictionary in Python
- Python | Dictionary initialization with common dictionary
- Python - Update dictionary with other dictionary
- Python - Filter dictionary values in heterogenous dictionary
- Python - Replace dictionary value from other dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python program to update a dictionary with the values from a dictionary list
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.

