Friday, December 27, 2024
Google search engine
HomeLanguagesWays to convert string to dictionary

Ways to convert string to dictionary

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




# Python implementation of converting
# a string into a dictionary
 
# initialising string
str = " Jan = January; Feb = February; Mar = March"
 
# At first the string will be splitted
# at the occurrence of ';' to divide items
# for the dictionaryand then again splitting
# will be done at occurrence of '=' which
# generates key:value pair for each item
dictionary = dict(subString.split("=") for subString in str.split(";"))
 
# printing the generated dictionary
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




# Python implementation of converting
# a string into a dictionary
 
# initialising first string
str1 = "Jan, Feb, March"
str2 = "January | February | March"
 
# splitting first string
# in order to get keys
keys = str1.split(", ")
 
# splitting second string
# in order to get values
values = str2.split("|")
 
# declaring the dictionary
dictionary = {}
 
# Assigning keys and its
# corresponding values in
# the dictionary
for i in range(len(keys)):
    dictionary[keys[i]] = values[i]
 
# printing the generated dictionary
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




# Python implementation of converting
# a string into a dictionary
 
# initialising first string
str1 = "Jan, Feb, March"
str2 = "January | February | March"
 
# splitting first string
# in order to get keys
keys = str1.split(", ")
 
# splitting second string
# in order to get values
values = str2.split("|")
 
# declaring the dictionary
dictionary = {}
 
# Assigning keys and its
# corresponding values in
# the dictionary
dictionary = dict(zip(keys, values))
 
# printing the generated dictionary
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




# Python implementation of converting
# a string into a dictionary
 
# importing ast module
import ast
 
# initialising string dictionary
str = '{"Jan" : "January", "Feb" : "February", "Mar" : "March"}'
 
# converting string into dictionary
dictionary = ast.literal_eval(str)
 
# printing the generated dictionary
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




# Python implementation of converting
# a string into a dictionary
 
# initialising string
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
# printing the generated dictionary
print(res)


Output

{' Jan ': ' January', ' Feb ': ' February', ' Mar ': ' March'}

Time Complexity: O(n)
Auxiliary Space: O(n)

RELATED ARTICLES

Most Popular

Recent Comments