Skip to content
Advertisement

Creating two separate “lists of lists” out of “list” & “list of lists”

I haven’t found a similar question with a solution here, so I’ll ask your help.

There are 2 lists, one of which is list of lists:

categories = ['APPLE', 'ORANGE', 'BANANA']
test_results = [['17.0', '12.0'], ['21.0', '15.0'], ['7.0', '6.0']]

As a final result I would like to split it by 2 separate lists of lists:

[['APPLE','17.0']['ORANGE','21.0']['BANANA','7.0']]
[['APPLE','12.0']['ORANGE','15.0']['BANANA','6.0']]

I tried to create loops but I have nothing to share. Any ideas are appreciated.

Advertisement

Answer

You can do it simply using list traversal and choosing element at index from first list and values from second list:

l1 = []
l2 = []
for i, values in enumerate(test_results):
    l1.append([categories[i], values[0]])
    l2.append([categories[i], values[1]])

print(l1)
print(l2)

# Output
# [['APPLE', '17.0'], ['ORANGE', '21.0'], ['BANANA', '7.0']]
# [['APPLE', '12.0'], ['ORANGE', '15.0'], ['BANANA', '6.0']]
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement