I am newbie in python. I need to merge 2 lists of lists based on the first element in each list. Here are the lists:
first_list = [['aaa', 551, 10972],
['bbb', 552, 10872],
['ccc', 553, 11103],
['ddd', 554, 10912]]
second_list = [['aaa', 240],
['bbb', 120],
['ccc', 325],
['ddd', 270]]
The code I have written:
join_list = []
for x in range(0, len(first_list)):
temp = [first_list[x], second_list[x][1]]
join_list.append(temp)
The following is the output:
join_list =
[[['aaa', 551, 10972], 240],
[['bbb', 552, 10872], 120],
[['ccc', 553, 11103], 325],
[['ddd', 554, 10912], 270]]
I need to flatten the list of the list in each element in join_list, e.g the first element in join_list[0] = ['histogram', 551, 10972, 240].
Also, if the order of the second list is change and if there is an addition of a sublist in the first element, the 2 lists is still can be merged based on the first index in each element. So, it works like the VLOOKUP or HLOOKUP in Ms.Excel. How can we do this?