Ways to convert string to dictionary

30 May, 2022
Dictionary is an unordered collection in Python which 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 key:value pair of the dictionary In this approach, the given string will be analysed and with the use of 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 stringstr = " 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 itemdictionary = dict(subString.split("=") for subString in str.split(";")) # printing the generated dictionaryprint(dictionary)
{' Feb ': ' February', ' Mar ': ' March', ' Jan ': ' January'}
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 stringstr1 = "Jan, Feb, March"str2 = "January | February | March" # splitting first string# in order to get keyskeys = str1.split(", ") # splitting second string# in order to get valuesvalues = str2.split("|") # declaring the dictionarydictionary = {} # Assigning keys and its# corresponding values in# the dictionaryfor i in range(len(keys)):    dictionary[keys[i]] = values[i] # printing the generated dictionaryprint(dictionary)
{'Jan': 'January ', 'Feb': ' February ', 'March': ' March'}
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 stringstr1 = "Jan, Feb, March"str2 = "January | February | March" # splitting first string# in order to get keyskeys = str1.split(", ") # splitting second string# in order to get valuesvalues = str2.split("|") # declaring the dictionarydictionary = {} # Merging all keys and values# to generate items for# the dictionarydictionary = dict(zip(keys, values)) # printing the generated dictionaryprint(dictionary)
{' March': ' March', 'Jan': 'January ', ' Feb': ' February '}
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 moduleimport ast # initialising string dictionarystr = '{"Jan" : "January", "Feb" : "February", "Mar" : "March"}' # converting string into dictionarydictionary = ast.literal_eval(str) # printing the generated dictionaryprint(dictionary)
{'Feb': 'February', 'Jan': 'January', 'Mar': 'March'}
nikhatkhan11
Python dictionary-programs
Python string-programs
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.