Ways to convert string to dictionary
Last Updated :
01 Feb, 2023
Dictionary is an unordered collection in Python that store data values like a map i.e., key: value pair. In order to convert a String into a dictionary, the stored string must be in such a way that key: value pair can be generated from it. This article demonstrates several ways of converting a string into a dictionary.
Method 1: Splitting a string to generate a key: value pair of the dictionary In this approach, the given string will be analyzed and with the use of the split() method, the string will be split in such a way that it generates the key: value pair for the creation of a dictionary. Below is the implementation of the approach.
Python3
str = " Jan = January; Feb = February; Mar = March"
dictionary = dict (subString.split( "=" ) for subString in str .split( ";" ))
print (dictionary)
|
Output
{' Jan ': ' January', ' Feb ': ' February', ' Mar ': ' March'}
Time Complexity: O(n), where n is the number of items in the input string.
Auxiliary Space: O(n), as it creates a dictionary with n key-value pairs.
Method 2: Using 2 strings to generate the key:value pair for the dictionary In this approach, 2 different strings will be considered and one of them will be used to generate keys and another one will be used to generate values for the dictionary. After manipulating both the strings the dictionary items will be created using those key:value pair. Below is the implementation of the approach.
Python3
str1 = "Jan, Feb, March"
str2 = "January | February | March"
keys = str1.split( ", " )
values = str2.split( "|" )
dictionary = {}
for i in range ( len (keys)):
dictionary[keys[i]] = values[i]
print (dictionary)
|
Output
{'Jan': 'January ', 'Feb': ' February ', 'March': ' March'}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using zip() method to combine the key:value pair extracted from 2 string In this approach, again 2 strings will be used, one for generating keys and another for generating values for the dictionary. When all the keys and values are stored, zip() method will be used to create key:value pair and thus generating the complete dictionary. Below is the implementation of the approach.
Python3
str1 = "Jan, Feb, March"
str2 = "January | February | March"
keys = str1.split( ", " )
values = str2.split( "|" )
dictionary = {}
dictionary = dict ( zip (keys, values))
print (dictionary)
|
Output
{'Jan': 'January ', 'Feb': ' February ', 'March': ' March'}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: If the string is itself in the form of string dictionary In this approach, a string which is already in the form of string dictionary i.e., the string has a dictionary expression is converted into a dictionary using ast.literal_eval() method. Below is the implementation of the approach.
Python3
import ast
str = '{"Jan" : "January", "Feb" : "February", "Mar" : "March"}'
dictionary = ast.literal_eval( str )
print (dictionary)
|
Output
{'Jan': 'January', 'Feb': 'February', 'Mar': 'March'}
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Method 5 : Using split(),index() and slicing
Python3
str = " Jan = January; Feb = February; Mar = March"
res = dict ()
x = str .split( ";" )
for i in x:
a = i[:i.index( "=" )]
b = i[i.index( "=" ) + 1 :]
res[a] = b
print (res)
|
Output
{' Jan ': ' January', ' Feb ': ' February', ' Mar ': ' March'}
Time Complexity: O(n)
Auxiliary Space: O(n)
Please Login to comment...