Ways to convert string to dictionary
Last Updated :
21 Jan, 2025
To convert a String into a dictionary, the stored string must be in such a way that a key: value pair can be generated from it.
For example, a string like "{'a': 1, 'b': 2, 'c': 3}" or "a:1, b:10" can be converted into a dictionary This article explores various methods to perform this conversion efficiently.
Using eval()
eval() function allows us to evaluate a string as Python code. If the string is formatted as a valid Python dictionary, we can directly convert it into a dictionary using this function.
Python
s = "{'a': 1, 'b': 2, 'c': 3}"
res = eval(s)
print(res)
Output{'a': 1, 'b': 2, 'c': 3}
Explanation:
- eval() function parses the string 's' as Python code.
- Since the string is formatted as a dictionary, it is directly converted into a dictionary object.
Let’s explore some more ways and see how we can convert string to dictionary.
Using json module
If the string is in JSON format, we can use the json module to parse it and convert it into a dictionary.
Python
import json
s = '{"a": 1, "b": 2, "c": 3}'
res = json.loads(s)
print(res)
Output{'a': 1, 'b': 2, 'c': 3}
Explanation:
- The json.loads function parses the string s as a JSON object.
- It converts the JSON object into a Python dictionary.
- This method is safe and ideal for strings in JSON format.
Using ast.literal_eval function
ast module provides the literal_eval function, which is similar to eval but safer. It only evaluates strings containing literals, such as dictionaries, lists, and numbers.
Python
import ast
s = "{'a': 1, 'b': 2, 'c': 3}"
res = ast.literal_eval(s)
print(res)
Output{'a': 1, 'b': 2, 'c': 3}
Explanation:
- ast.literal_eval() function parses the string 's' as a literal.
- It converts the string into a dictionary if it is formatted as one.
Using string manipulation
If the string is not formatted as a dictionary, we can manually process it using string operations to convert it into a dictionary.
Python
s = "a:1, b:2, c:3"
res = {x.split(":")[0]: int(x.split(":")[1]) for x in s.split(", ")}
print(res)
Output{'a': 1, 'b': 2, 'c': 3}
Explanation:
- We split the string into key-value pairs using the comma and space as a separator.
- Each pair is further split using the colon to extract the key and value.
- The result is built as a dictionary using a dictionary comprehension.
Similar Reads
How To Convert Python Dictionary To JSON? In Python, a dictionary stores information using key-value pairs. But if we want to save this data to a file, share it with others, or send it over the internet then we need to convert it into a format that computers can easily understand. JSON (JavaScript Object Notation) is a simple format used fo
6 min read
Convert Unicode String to Dictionary in Python Python's versatility shines in its ability to handle diverse data types, with Unicode strings playing a crucial role in managing text data spanning multiple languages and scripts. When faced with a Unicode string and the need to organize it for effective data manipulation, the common task is convert
2 min read
Python - Converting list string to dictionary Converting a list string to a dictionary in Python involves mapping elements from the list to key-value pairs. A common approach is pairing consecutive elements, where one element becomes the key and the next becomes the value. This results in a dictionary where each pair is represented as a key-val
3 min read
Ways to create a dictionary of Lists - Python A dictionary of lists is a type of dictionary where each value is a list. These dictionaries are commonly used when we need to associate multiple values with a single key.Initialize a Dictionary of ListsThis method involves manually defining a dictionary where each key is explicitly assigned a list
3 min read
Python - Convert Dictionary Object into String In Python, there are situations where we need to convert a dictionary into a string format. For example, given the dictionary {'a' : 1, 'b' : 2} the objective is to convert it into a string like "{'a' : 1, 'b' : 2}". Let's discuss different methods to achieve this:Using strThe simplest way to conver
2 min read