Python | Convert key-value pair comma separated string into dictionary

Given a string, with different key-value pairs separated with commas, the task is to convert that string into the dictionary. These types of problems are common in web development where we fetch arguments from queries or get a response in the form of strings. Given below are a few methods to solve the task.

Method #1: Using dictionary comprehension

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# converting comma separated string
# into dictionary
  
# Initialising string
ini_string1 = 'name = akshat, course = btech, branch = computer'
  
# Printing initial string
print ("Initial String", ini_string1)
  
# Converting string into dictionary
# using dict comprehension
res = dict(item.split("=") for item in ini_string1.split(", "))
          
# Printing resultant string
print ("Resultant dictionary", str(res))
     

chevron_right


Output:

Initial String name = akshat, course = btech, branch = computer
Resultant dictionary {‘branch ‘: ‘ computer’, ‘name ‘: ‘ akshat’, ‘course ‘: ‘ btech’}

 
Method #2: Using Map and lambda

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# converting comma seperated string
# into dictionary
  
# Initialising string
ini_string1 = 'name = akshat, course = btech, branch = computer'
  
# Printing initial string
print ("Initial String", ini_string1)
  
# Converting string into dictionary
# using map and lambda
res = dict(map(lambda x: x.split('='), ini_string1.split(', ')))
          
# Printing resultant string
print ("Resultant dictionary", str(res))
     

chevron_right


Output:

Initial String name = akshat, course = btech, branch = computer
Resultant dictionary {‘course ‘: ‘ btech’, ‘name ‘: ‘ akshat’, ‘branch ‘: ‘ computer’}

 
Method #3: Using eval() function

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# converting comma seperated string
# into dictionary
  
# Initialising string
ini_string1 = 'name ="akshat", course ="btech", branch ="computer"'
  
# Printing initial string
print ("Initial String", ini_string1)
  
# Converting string into dictionary
# using eval
res = eval('dict('+ini_string1+')')
          
# Printing resultant string
print ("Resultant dictionary", str(res))
   

chevron_right


Output:

Initial String name =”akshat”, course =”btech”, branch =”computer”
Resultant dictionary {‘course’: ‘btech’, ‘name’: ‘akshat’, ‘branch’: ‘computer’}



My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

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.