Shown below is a method for converting a list of dictionaries to a dictionary of lists in Python, when the original list of dictionaries share some (but not all) of the same keys:
# Create dict list
d1 = {x: x + 2 for x in range(0, 20, 2)}
d2 = {x: x * 4 for x in range(0, 20, 3)}
d3 = {x: x ** 2 for x in range(0, 20, 5)}
dict_list = [d1, d2, d3]
# Convert to list dict
key_set = {k for d in dict_list for k in d.keys()}
val_list = lambda k: [d.get(k) for d in dict_list if d.get(k) is not None]
list_dict = {k: val_list(k) for k in key_set}
# Print results
print(d1, d2, d3, sep="\n")
print(key_set)
print(list_dict)Output:
{0: 2, 2: 4, 4: 6, 6: 8, 8: 10, 10: 12, 12: 14, 14: 16, 16: 18, 18: 20}
{0: 0, 3: 12, 6: 24, 9: 36, 12: 48, 15: 60, 18: 72}
{0: 0, 5: 25, 10: 100, 15: 225}
{0, 2, 3, 4, 5, 6, 8, 9, 10, 12, 14, 15, 16, 18}
{0: [2, 0, 0], 2: [4], 3: [12], 4: [6], 5: [25], 6: [8, 24], 8: [10], 9: [36], 10: [12, 100], 12: [14, 48], 14: [16], 15: [60, 225], 16: [18], 18: [20, 72]}