Convert String to Nested Dictionaries Last Updated : 22 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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_eval function from the ast module to safely evaluate the string and convert it to a nested dictionary. Python import ast # Input string s = "{'a': {'b': {'c': 1}}}" # Converting string to nested dictionary d = ast.literal_eval(s) print(d) Output{'a': {'b': {'c': 1}}} Explanation:ast.literal_eval function parses the string and evaluates it as a Python literal, converting it to a nested dictionary.This method is safe and handles nested structures efficiently.Let's explore some more ways and see how we can convert string to nested dictionaries.Table of ContentUsing json.loads with String ReplacementUsing eval()Using json.loads with String ReplacementThis method uses the json.loads function after replacing single quotes with double quotes to match JSON format requirements. Python import json # Input string s = "{'a': {'b': {'c': 1}}}" # Converting string to nested dictionary d = json.loads(s.replace("'", '"')) print(d) Output{'a': {'b': {'c': 1}}} Explanation:JSON requires keys and strings to be enclosed in double quotes. The replace method replaces single quotes with double quotes.The json.loads function then parses the string into a nested dictionary.Using eval()This method uses the built-in eval() function to directly evaluate the string into a Python object. Python # Input string s = "{'a': {'b': {'c': 1}}}" # Converting string to nested dictionary d = eval(s) print(d) Output{'a': {'b': {'c': 1}}} Explanation:eval() function evaluates the string as Python code and converts it into a nested dictionary.While this method works, it is not recommended for untrusted inputs due to security risks. Comment More infoAdvertise with us Next Article Convert String to Nested Dictionaries M manjeet_04 Follow Improve Article Tags : Python Python Programs Python dictionary-programs Python-nested-dictionary Practice Tags : python Similar Reads Convert Nested Dictionary to List in Python In this article, weâll explore several methods to Convert Nested Dictionaries to a List in Python. List comprehension is the fastest and most concise way to convert a nested dictionary into a list.Pythona = { "a": {"x": 1, "y": 2}, "b": {"x": 3, "y": 4}, } # Convert nested dictionary to a list of li 3 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 List to List of dictionaries We are given a lists with key and value pair we need to convert the lists to List of dictionaries. For example we are given two list a=["name", "age", "city"] and b=[["Geeks", 25, "New York"], ["Geeks", 30, "Los Angeles"], ["Geeks", 22, "Chicago"]] we need to convert these keys and values list into 4 min read Python - Convert Flat dictionaries to Nested dictionary Sometimes, while working with records, we can have a problem in which we need to perform the task of conversion of multiple flat dictionaries to a single nested dictionary. This can have applications in many domains in which data is used extensively. Let's discuss certain ways by which we can conver 4 min read Python - Convert Nested dictionary to Mapped Tuple The task is to convert a nested dictionary into a mapping where each key's values are transformed into tuples. This involves taking the inner dictionaries and creating tuples for each key-value pair across the nested structure. If a key appears in multiple inner dictionaries, its values should be gr 4 min read Like