How to convert a String representation of a Dictionary to a dictionary in Python?



To convert a string represented dictionary to an original dictionary, we can use built-in functions like eval(), json.load(), and ast.literal_eval(). Initially, we must ensure that the string contains a valid representation of the dictionary.

Using the eval() function

The eval() is a Python built-In function that takes a string argument, parses it as a code expression, and evaluates the expression. If the input expression contains a string representation of a dictionary, then the eval() method returns it as a normal Python dictionary.

Syntax

Following is the syntax of the eval() function in Python -

eval(expression[, globals[, locals]]) 

Example

Using the eval() function is the easiest way but it is dangerous, because it may execute any malicious code injected inside the string.

String_dict = "{'1': 1, '2': 2, '3': 3}" print("Input string represented dictionary: ",String_dict) print(type(String_dict)) # use the eval method to convert the expression dict_ = eval(String_dict) print("Output: ", dict_) print(type(dict_)) 

Following is an output of the above code -

Input string represented dictionary: {'1': 1, '2': 2, '3': 3} <class 'str'> Output: {'1': 1, '2': 2, '3': 3} <class 'dict'> 

Using the ast.literal_eval() function

Abstract Syntax Tree (AST) is a Python module that provides a method called literal_eval(), which is used to convert strings to dictionaries efficiently.

This method never executes the code; it will parse the expression and return only if it is a literal, so it is the safest way to convert a string.

Syntax

Following is the syntax of the ast.literal_eval() function in Python -

ast.literal_eval ( node_or_string ) 

Example

Let us see an example of this function. To use the literal_eval() function, initially, we need to import the ast package.

import ast string_dict = "{'a': 1, 'b': 2, 'c': 3}" print("Input string represented dictionary: ",string_dict) print(type(string_dict)) # convert the string dict_ = ast.literal_eval(string_dict) print("Output: ", dict_) print(type(dict_)) 

Following is an output of the above code -

Input string represented dictionary: {'a': 1, 'b': 2, 'c': 3} <class 'str'> Output: {'a': 1, 'b': 2, 'c': 3} <class 'dict'> 

As we can see in the above output, the ast.literal_eval() method successfully evaluated the string and returned the Python dictionary object.

Example

Following is another example of the ast.literal_eval() function -

import ast stringA = '{"Mon" : 3, "Wed" : 5, "Fri" : 7}' # Given string dictionary print("Given string : \n",stringA) # using json.loads() res = ast.literal_eval(stringA) # Result print("The converted dictionary : \n",res) 

Following is an output of the above code -

Given string : {"Mon" : 3, "Wed" : 5, "Fri" : 7} The converted dictionary : {'Mon': 3, 'Wed': 5, 'Fri': 7} 

Using the json.loads() function

The json.loads() is a built-in Python function that converts a valid string to a Python object. Let's use the json.loads() method to convert a string representation of a dictionary into the original dictionary object.

Syntax

Following is the syntax of the json.loads() function in Python -

json.loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw) 

Before using the loads() method, we need to import the json library first.

Example

In the following example, we can see that the loads() function returned a dictionary, and we verified that by using the type() method.

import json d = '{"Age": 7, "Gender": "Female", "Name": "Jennifer"}' print("Input string represented dictionary: ",d) print(type(d)) python_dict = json.loads(d) print("Output: ", python_dict) print(type(python_dict)) 

Following is an output of the above code ?

Input string represented dictionary: {"Age": 7, "Gender": "Female", "Name": "Jennifer"} <class 'str'> Output: {'Age': 7, 'Gender': 'Female', 'Name': 'Jennifer'} <class 'dict'> 

Example

Following is another example of the json.loads() function -

import json stringA = '{"Mon" : 3, "Wed" : 5, "Fri" : 7}' # Given string dictionary print("Given string : \n",stringA) # using json.loads() res = json.loads(stringA) # Result print("The converted dictionary : \n",res) 

Following is an output of the above code -

Given string : {"Mon" : 3, "Wed" : 5, "Fri" : 7} The converted dictionary : {'Mon': 3, 'Wed': 5, 'Fri': 7} 

Note: json.loads() will raise an error if we have single quotes as a part of our keys or values.

Updated on: 2025-05-29T11:00:02+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements