I have a list of lists that looks as follows.
names_table = [["Jerry", "A"],[ "Jerry", "B"], ["Jerry", "C"], ["George","C"], ["George","D"], ["George","E"], ["Elaine","A"], ["Kramer","E"], ["Kramer","F"] ]
My objective is to obtain an output list of lists in which each name only appears once, such as:
[["Jerry", ["A", "B", "C"]],["George", ["C", "D", "E"]], ["Elaine", ["A"]], ["Kramer", ["E", "F"]]]
My attempt:
I've approached this by deriving a list that contains all names just once
names = ["Jerry", "George", "Elaine", "Kramer"]
And then two for
loops with a while statement, but the program enters into an infinite loop.
How could I fix this?
output = []
for row in names_table:
for name in names:
while name in row[0]:
letters = []
letters.append(row[1])
output_row = [name, letters]
output.append(output_row)
break