Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Convert Dictionary to Concatenated String

  • Last Updated : 14 Jul, 2021

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. Let’s discuss certain ways in which this task can be performed.

Method 1: Using an empty string + for loop

In this method, we will use a for loop to iterate through the dictionary object and keep on adding the key: value pairs to the empty string.

Python3




# Python3 code to demonstrate working of
# Convert Dictionary to Concatenated String
# Using an empty string + for loop
 
# initializing dictionary
test_dict = {'gfg': 1, 'is': 2, 'best': 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Create an empty string
res = ' '
 
# Convert Dictionary to Concatenated String
# Using for loop and empty string
for item in test_dict:
    res += item + str(test_dict[item])
 
# printing result
print("The dictionary after concatenation is : " + str(res))

Output:

The original dictionary is : {'gfg': 1, 'is': 2, 'best': 3}
The dictionary after concatenation is :  gfg1is2best3

Method 2 : 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




# 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))

Output :

The original dictionary is : {'gfg': 1, 'best': 2, 'is': 3}
The dictionary after concatenation is : gfg1best2is3

 
Method 3 : 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




# 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))

Output :

The original dictionary is : {'gfg': 1, 'best': 2, 'is': 3}
The dictionary after concatenation is : gfg1best2is3


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!