Interconversions of data types have been discussed many times and have been quite a popular problem to solve. This article discusses yet another problem of interconversion of dictionary, in string format to a dictionary. Let’s discuss certain ways in which this can be done.
Method #1 : Using json.loads()
This task can easily be performed using the inbuilt function of loads of json library of python which converts the string of valid dictionary into json form, dictionary in Python.
# Python3 code to demonstrate # convert dictionary string to dictionary # using json.loads() import json # initializing string test_string = '{"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}' # printing original string print("The original string : " + str(test_string)) # using json.loads() # convert dictionary string to dictionary res = json.loads(test_string) # print result print("The converted dictionary : " + str(res)) |
The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
Method #2 : Using ast.literal_eval()
The above method can also be used to perform a similar conversion. Function safer than the eval function and can be used for interconversion of all data types other than dictionary as well.
# Python3 code to demonstrate # convert dictionary string to dictionary # using ast.literal_eval() import ast # initializing string test_string = '{"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}' # printing original string print("The original string : " + str(test_string)) # using ast.literal_eval() # convert dictionary string to dictionary res = ast.literal_eval(test_string) # print result print("The converted dictionary : " + str(res)) |
The original string : {"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}
The converted dictionary : {'Nikhil': 1, 'Akshat': 2, 'Akash': 3}
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python - Convert Dictionary Value list to Dictionary List
- Python | Convert dictionary object into string
- Python | Convert byteString key:value pair of dictionary to String
- Python | Convert key-value pair comma separated string into dictionary
- Python - Convert Dictionary to Concatenated String
- Python - Convert key-value String to dictionary
- Python - Convert String List to Key-Value List dictionary
- Python program to split the string and convert it to dictionary
- Ways to convert string to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Regular Dictionary vs Ordered Dictionary in Python
- Python | Dictionary initialization with common dictionary
- Python - Update dictionary with other dictionary
- Python - Filter dictionary values in heterogenous dictionary
- Python - Replace dictionary value from other dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python program to update a dictionary with the values from a dictionary list
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.

