# Python – Convert Value list elements to List records

Sometimes, while working with Python dictionaries, we can have a problem in which we need to convert the nested list values to individual records to include another level of nesting to accommodate data. This kind of problem can have applications in data domains such as web development and Machine Learning. Let’s discuss certain ways in which this task can be performed.

Input : test_dict = {‘gfg’ : [4, 5], ‘best’ : [8, 10, 7, 9]}
Output : {‘best’: [{8: []}, {10: []}, {7: []}, {9: []}], ‘gfg’: [{4: []}, {5: []}]}

Input : test_dict = {‘gfg’ : }
Output : {‘gfg’: [{7: []}]}

Method #1 : Using loop + `enumerate()`
The combination of above functions can be used to solve this problem. This is brute force approach to this in which we iterate for all list and create records list for each value.

 `# Python3 code to demonstrate working of  ` `# Convert Value list elements to List records ` `# Using loop + enumerate() ` ` `  `# initializing dictionary ` `test_dict ``=` `{``'gfg'` `: [``4``, ``5``], ``'is'` `: [``8``], ``'best'` `: [``10``]} ` ` `  `# printing original dictionary ` `print``(``"The original dictionary : "` `+` `str``(test_dict)) ` ` `  `# Convert Value list elements to List records ` `# Using loop + enumerate() ` `for` `ele ``in` `test_dict.values(): ` `    ``for` `idx, val ``in` `enumerate``(ele): ` `        ``ele[idx] ``=` `{val: []} ` ` `  `# printing result  ` `print``(``"The converted dictionary is : "` `+` `str``(test_dict))  `

Output :

The original dictionary : {‘best’: , ‘is’: , ‘gfg’: [4, 5]}
The converted dictionary is : {‘best’: [{10: []}], ‘is’: [{8: []}], ‘gfg’: [{4: []}, {5: []}]}

Method #2 : Using dictionary comprehension + `items()`
This is shorthand method to solve this problem. In this, the dictionary is created using dictionary comprehension and dictionary items are extracted using items().

 `# Python3 code to demonstrate working of  ` `# Convert Value list elements to List records ` `# Using dictionary comprehension + items() ` ` `  `# initializing dictionary ` `test_dict ``=` `{``'gfg'` `: [``4``, ``5``], ``'is'` `: [``8``], ``'best'` `: [``10``]} ` ` `  `# printing original dictionary ` `print``(``"The original dictionary : "` `+` `str``(test_dict)) ` ` `  `# Convert Value list elements to List records ` `# Using dictionary comprehension + items() ` `res ``=` `{key: [{val: []} ``for` `val ``in` `sub] ``for` `key, sub ``in` `test_dict.items()} ` ` `  `# printing result  ` `print``(``"The converted dictionary is : "` `+` `str``(res))  `

Output :

The original dictionary : {‘best’: , ‘is’: , ‘gfg’: [4, 5]}
The converted dictionary is : {‘best’: [{10: []}], ‘is’: [{8: []}], ‘gfg’: [{4: []}, {5: []}]}

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.