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
# 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)) |
Output
Initial String name = akshat, course = btech, branch = computer Resultant dictionary {'name ': ' akshat', 'course ': ' btech', 'branch ': ' computer'}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using Map and lambda
Python3
# 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 map and lambda res = dict ( map ( lambda x: x.split( '=' ), ini_string1.split( ', ' ))) # Printing resultant string print ( "Resultant dictionary" , str (res)) |
Output
Initial String name = akshat, course = btech, branch = computer Resultant dictionary {'name ': ' akshat', 'course ': ' btech', 'branch ': ' computer'}
Time Complexity: O(n)
Space Complexity: O(n)
Method #3: Using eval() function
Python3
# 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 eval res = eval ( 'dict(' + ini_string1 + ')' ) # Printing resultant string print ( "Resultant dictionary" , str (res)) |
Output
Initial String name ="akshat", course ="btech", branch ="computer" Resultant dictionary {'name': 'akshat', 'course': 'btech', 'branch': 'computer'}
Time Complexity: O(n)
Space Complexity: O(n)
Method #4 : Using split() and index() methods
Python3
# 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 res = dict () x = ini_string1.split( "," ) for i in x: a = i[:i.index( "=" )] b = i[i.index( "=" ) + 1 :] res[ str (a)] = str (b) # Printing resultant string print ( "Resultant dictionary" , str (res)) |
Output
Initial String name = akshat, course = btech, branch = computer Resultant dictionary {'name ': ' akshat', ' course ': ' btech', ' branch ': ' computer'}
Time Complexity: O(n)
Space Complexity: O(n)
Please Login to comment...