Python | Converting String content to dictionary

Sometimes, we are fed with string and we may have to convert it’s content to dictionary. The string may have a specified format that could be key-value convertible. This type of problem is quite common in Machine Learning domain. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using split() + dictionary comprehension
The combination of above method can be used to perform this particular task. This is 2 process method. In 1st step, the string is converted to list using split and then converted back to dictionary using dictionary comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Converting String content to dictionary
# Using dictionary comprehension + split()
  
# initializing string 
test_str = "Gfg = 1, Good = 2, CS = 3, Portal = 4"
  
# printing original string 
print("The original string is : " + test_str)
  
# Using dictionary comprehension + split()
# Converting String content to dictionary
res = {key: int(val) for key, val in (item.split('=')
                   for item in test_str.split(', '))}
  
# printing result 
print("The newly created dictionary : " + str(res))

chevron_right


Output :

The original string is : Gfg = 1, Good = 2, CS = 3, Portal = 4
The newly created dictionary : {' CS ': 3, 'Gfg ': 1, ' Portal ': 4, ' Good ': 2}

 

Method #2 : Using eval()
This particular problem can be solved using the inbuilt function eval which internally evaluates the string and converts the string to dictionary depending upon the condition.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Converting String content to dictionary
# Using eval()
  
# initializing string 
test_str = "Gfg = 1, Good = 2, CS = 3, Portal = 4"
  
# printing original string 
print("The original string is : " + test_str)
  
# Using eval()
# Converting String content to dictionary
res = eval('dict(% s)' % test_str)
  
# printing result 
print("The newly created dictionary : " + str(res))

chevron_right


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.




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.