lst1 = [('test1', 0.2), ('test7', 0.2)]
lst2 = [('test1', 5.2), ('test2', 11.1), ('test7', 0.2)]
lst3 = [('test1', 19.2), ('test2', 12.1), ('test7', 19.2), ('test9', 15.1)]
# create a list containing all these lists
lst_of_lists = [lst1, lst2, lst3]
# Create a dictionary where keys are the test1, test2, test3...
# and the values are [0.0, 0.0, ....] as many as the number of lists
output_dict = {
name:[0.0]*len(lst_of_lists) for lst in lst_of_lists
for (name, value) in lst
}
# Now, 'test1' in lst1 has to modify output_dict['test1'][0]
# 'test1' in lst2 modifies output_dict['test1'][1]
# and so on...
for idx, lst in enumerate(lst_of_lists):
for key, value in lst:
output_dict[key][idx] = value
# this gives you a dictionary
print("output_dict: ", output_dict)
# you can convert it back to a list with .items(), and since you want it sorted
print("dict_as_list: ", sorted(output_dict.items()))
# since you want the keys and values together in the same tuple
output_list = [tuple([key] + val_list) for key, val_list in output_dict.items()]
# and sort that
output_list = sorted(output_list)
print("output_list: ", output_list)
Output:
output_dict: {'test1': [0.2, 5.2, 19.2], 'test7': [0.2, 0.2, 19.2], 'test2': [0.0, 11.1, 12.1], 'test9': [0.0, 0.0, 15.1]}
dict_as_list: [('test1', [0.2, 5.2, 19.2]), ('test2', [0.0, 11.1, 12.1]), ('test7', [0.2, 0.2, 19.2]), ('test9', [0.0, 0.0, 15.1])]
output_list: [('test1', 0.2, 5.2, 19.2), ('test2', 0.0, 11.1, 12.1), ('test7', 0.2, 0.2, 19.2), ('test9', 0.0, 0.0, 15.1)]