Python – Convert key-value String to dictionary

Sometimes, while working with Python strings, we can have problem in which we need to convert a string key-value pairs to dictionary. This can have applications in which we are working with string data and needs to be converted. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using map() + split() + loop
The combination of above functionalities can be used to perform this task. In this, we perform the conversion of key-value pair to dictionary using map and splitting key-value pairs is done using split().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert key-value String to dictionary
# Using map() + split() + loop
  
# initializing string
test_str = 'gfg:1, is:2, best:3'
  
# printing original string
print("The original string is : " + str(test_str))
  
# Convert key-value String to dictionary
# Using map() + split() + loop
res = []
for sub in test_str.split(', '):
    if ':' in sub:
        res.append(map(str.strip, sub.split(':', 1)))
res = dict(res)
  
# printing result 
print("The converted dictionary is : " + str(res)) 

chevron_right


Output :

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}

 

Method #2 : Using dict() + generator expression + split() + map()
This is yet another way in which this problem can be solved. In this, we perform the task in similar way as above but in 1 liner way using dict() and generator expression.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert key-value String to dictionary
# Using dict() + generator expression + split() + map()
  
# initializing string
test_str = 'gfg:1, is:2, best:3'
  
# printing original string
print("The original string is : " + str(test_str))
  
# Convert key-value String to dictionary
# Using dict() + generator expression + split() + map()
res = dict(map(str.strip, sub.split(':', 1)) for sub in test_str.split(', ') if ':' in sub)
  
# printing result 
print("The converted dictionary is : " + str(res)) 

chevron_right


Output :

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.