Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 714
Oleg Kazanskyi Avatar asked Dec 18 '22 12:12

Oleg Kazanskyi


1 Answers

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']]
like image 195
Astik Anand Avatar answered May 03 '23 18:05

Astik Anand