![]() |
VOOZH | about |
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'}].
We can first split the values in the dictionary using split(':') and then use zip() to pair corresponding elements from all lists.
[{'Gfg': '1', 'best': '4'}, {'Gfg': '2', 'best': '8'}, {'Gfg': '3', 'best': '11'}]
Explanation:
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.
[{'Gfg': '1', 'best': '4'}, {'Gfg': '2', 'best': '8'}, {'Gfg': '3', 'best': '11'}]
Explanation: