I need to convert one dictionary, whose values are in a list to a list of dictionaries.
For example, the following dictionary:
only_dict = {'First': [1, 2], 'Second': [3, 4]}
Should have the following output, where values are no longer in a list.
out_lst = [{'First': 1, 'Second': 3}, {'First': 2, 'Second': 4}]
>Solution :
A simple version for any length of the lists. First, get the maximum list length:
count = max(*[len(x) for x in only_dict])
Construct each of the dictionaries one by one:
out_lst = [{k: v[i] for k, v in only_dict.items() if len(v) >= i} for i in range(count)]