Convert Dictionary String Values to List of Dictionaries - Python Last Updated : 31 Jan, 2025 Comments Improve Suggest changes Like Article Like Report We are given a dictionary where the values are strings containing delimiter-separated values and the task is to split each string into separate values and convert them into a list of dictionaries, with each dictionary containing a key-value pair for each separated value. For example: consider this dictionary {"Gfg": "1:2:3", "best": "4:8:11"} then the output should be [{'Gfg': '1', 'best': '4'}, {'Gfg': '2', 'best': '8'}, {'Gfg': '3', 'best': '11'}].Using zip() and split()We can first split the values in the dictionary using split(':') and then use zip() to pair corresponding elements from all lists. Python d = {"Gfg": "1:2:3", "best": "4:8:11"} # Splitting values and using zip to pair corresponding elements res = [dict(zip(d.keys(), vals)) for vals in zip(*[v.split(':') for v in d.values()])] print(res) Output[{'Gfg': '1', 'best': '4'}, {'Gfg': '2', 'best': '8'}, {'Gfg': '3', 'best': '11'}] Explanation:We use split(':') to break each string into a list of values.zip(*[v.split(':') for v in d.values()]) transposes these lists thus pairing corresponding elements together.dict(zip(d.keys(), vals)) creates a dictionary for each paired group hence, final result is a list of dictionaries.Using enumerate and split()This method uses a loop to iterate through the dictionary and splits the string values by the : delimiter and then each split value is assigned to a separate dictionary. The result is a list of dictionaries where each dictionary contains key-value pairs corresponding to the split values. Python from collections import defaultdict d1 = {"Gfg": "1:2:3", "best": "4:8:11"} d2 = defaultdict(dict) for k in d1: for i, v in enumerate(d1[k].split(':')): d2[i][k] = v r = [d2[i] for i in d2] print(r) Output[{'Gfg': '1', 'best': '4'}, {'Gfg': '2', 'best': '8'}, {'Gfg': '3', 'best': '11'}] Explanation:dictionary d is iterated using a loop that splits each string by : and maps the results to temporary dictionaries indexed by split positions.temporary dictionaries are then collected into a final list r containing dictionaries with split values for each key. Comment More infoAdvertise with us Next Article Convert Dictionary String Values to List of Dictionaries - Python M manjeet_04 Follow Improve Article Tags : Python Python Programs Python dictionary-programs Practice Tags : python Similar Reads Python - Convert list of dictionaries to Dictionary Value list We are given a list of dictionaries we need to convert it to dictionary. For example, given a list of dictionaries: d = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6}], the output should be: {'a': [1, 3, 5], 'b': [2, 4, 6]}.Using Dictionary ComprehensionUsing dictionary comprehension, we can 3 min read Convert Dictionary Value list to Dictionary List Python Sometimes, while working with Python Dictionaries, we can have a problem in which we need to convert dictionary list to nested records dictionary taking each index of dictionary list value and flattening it. This kind of problem can have application in many domains. Let's discuss certain ways in whi 9 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 Python - Convert String to List of dictionaries Given List of dictionaries in String format, Convert into actual List of Dictionaries. Input : test_str = ["[{'Gfg' : 3, 'Best' : 8}, {'Gfg' : 4, 'Best' : 8}]"] Output : [[{'Gfg': 3, 'Best': 8}, {'Gfg': 4, 'Best': 8}]] Explanation : String converted to list of dictionaries. Input : test_str = ["[{'G 4 min read Python - Convert String List to Key-Value List dictionary Given a string, convert it to key-value list dictionary, with key as 1st word and rest words as value list. Input : test_list = ["gfg is best for geeks", "CS is best subject"] Output : {'gfg': ['is', 'best', 'for', 'geeks'], 'CS': ['is', 'best', 'subject']} Explanation : 1st elements are paired with 8 min read Like