Splitting the String and Converting it to Dictionary Last Updated : 21 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In Python, we may need to convert a formatted string into a dictionary. For example, given the string "a:1, b:2, c:3", we want to split the string by its delimiters and convert it into a dictionary. Let's explore various methods to accomplish this task.Using Dictionary ComprehensionWe can split the string by commas and colons and construct a dictionary directly using dictionary comprehension. Python # Input string s = "a:1, b:2, c:3" # Converting string to dictionary d = {i.split(":")[0]: int(i.split(":")[1]) for i in s.split(", ")} # Output dictionary print(d) Output{'a': 1, 'b': 2, 'c': 3} Explanation:The string is first split by, to get key-value pairs.Each pair is further split by : to extract keys and values.A dictionary is constructed directly using dictionary comprehension.Let's explore some more ways and see how we can split the string and convert it to dictionary.Table of ContentUsing map with Dictionary ComprehensionUsing For LoopUsing eval()Using map with Dictionary ComprehensionThis method uses the map() function to process each key-value pair, making the code slightly more modular. Python # Input string s = "a:1, b:2, c:3" # Converting string to dictionary d = {k: int(v) for k, v in map(lambda x: x.split(":"), s.split(", "))} # Output dictionary print(d) Output{'a': 1, 'b': 2, 'c': 3} Explanation:map() function is used to split each key-value pair into a tuple of key and value.A dictionary comprehension constructs the dictionary from the tuples.This method is modular and avoids repetitive splitting.Using For LoopWe can use a simple for loop to manually split and construct the dictionary. Python # Input string s = "a:1, b:2, c:3" # Converting string to dictionary pairs = s.split(", ") d = {} for pair in pairs: key, value = pair.split(":") d[key] = int(value) # Output dictionary print(d) Output{'a': 1, 'b': 2, 'c': 3} Explanation:The string is first split into key-value pairs using , .Each pair is split by : and added to the dictionary in the loop.This method is straightforward and easy to understand but less concise than other methods.Using eval()This method works if the input string is formatted like a Python dictionary. It is less safe but useful in specific cases. Python # Input string s = "{'a': 1, 'b': 2, 'c': 3}" # Converting string to dictionary d = eval(s) # Output dictionary print(d) Output{'a': 1, 'b': 2, 'c': 3} Explanation:eval() function directly interprets the string as Python code.This method only works for strings formatted as Python dictionary literals.It is not recommended for untrusted input due to security risks. Comment More infoAdvertise with us Next Article Splitting the String and Converting it to Dictionary M manjeet_04 Follow Improve Article Tags : Python Python Programs Python dictionary-programs Practice Tags : python Similar Reads Converting String Content to Dictionary - Python In Python, a common requirement is to convert string content into a dictionary. For example, consider the string "{'a': 1, 'b': 2, 'c': 3}". The task is to convert this string into a dictionary like {'a': 1, 'b': 2, 'c': 3}. This article demonstrates several ways to achieve this in Python.Using ast. 2 min read Convert Dictionary to String List in Python The task of converting a dictionary to a string list in Python involves transforming the key-value pairs of the dictionary into a formatted string and storing those strings in a list. For example, consider a dictionary d = {1: 'Mercedes', 2: 'Audi', 3: 'Porsche', 4: 'Lambo'}. Converting this to a st 3 min read Python - Convert Dictionary to Concatenated String In Python, sometimes we need to convert a dictionary into a concatenated string. The keys and values of the dictionary are combined in a specific format to produce the desired string output. For example, given the dictionary {'a': 1, 'b': 2, 'c': 3}, we may want the string "a:1, b:2, c:3". Let's dis 3 min read Convert String Dictionary to Dictionary in Python The goal here is to convert a string that represents a dictionary into an actual Python dictionary object. For example, you might have a string like "{'a': 1, 'b': 2}" and want to convert it into the Python dictionary {'a': 1, 'b': 2}. Let's understand the different methods to do this efficiently.Us 2 min read Convert String to Nested Dictionaries In Python, sometimes a string represents nested dictionary data. For example, the string might look like "{'a': {'b': {'c': 1}}}". The task is to convert this string into an actual nested dictionary. Let's explore several methods to achieve this.Using ast.literal_evalThis method uses the literal_eva 2 min read Like