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
# 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)) |
Initial String name = akshat, course = btech, branch = computer
Resultant dictionary {‘branch ‘: ‘ computer’, ‘name ‘: ‘ akshat’, ‘course ‘: ‘ btech’}
Method #2: Using Map and lambda
# 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)) |
Initial String name = akshat, course = btech, branch = computer
Resultant dictionary {‘course ‘: ‘ btech’, ‘name ‘: ‘ akshat’, ‘branch ‘: ‘ computer’}
Method #3: Using eval() function
# 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)) |
Initial String name =”akshat”, course =”btech”, branch =”computer”
Resultant dictionary {‘course’: ‘btech’, ‘name’: ‘akshat’, ‘branch’: ‘computer’}
Recommended Posts:
- Python | Convert byteString key:value pair of dictionary to String
- Python | Convert list of strings to space separated string
- Python | Convert string dictionary to dictionary
- Python | Convert dictionary object into string
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert flattened dictionary into nested dictionary
- Add a key:value pair to dictionary in Python
- Python | Get random dictionary pair
- Python | Convert a set into dictionary
- Python | Convert Tuples to Dictionary
- Python | Convert two lists into a dictionary
- Python | Convert a list to dictionary
- Python | Convert a list of Tuples into Dictionary
- Python | Convert dictionary to list of tuples
- Python | Convert list of tuple into dictionary
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.



