I want to remove the first index of each list within a list of a list. And create one list for each list within the list as shown below. E.g my output is:
List = [[[0.75, 4], [0.43, 2], [0.29, 1], [0.03, 3]], [[0.77, 4], [0.77, 3], [0.55, 1], [0.45, 2]]]
And I want the output to be:
newList = [[4,2,1,3], [4,3,1,2]]
Im getting confused due to the list within a list of a list.
CodePudding user response:
Try nested list comprehension:
newList = [[elements[1] for elements in nested] for nested in List]
The inner list comprehension takes the second element of the list (elements), the outer does this for all the outer lists.
CodePudding user response:
One way to be more clear to you what is happening would be as follows:
new_list = []
for outer_item in List:
inner_list = []
for inner_item in outer_item:
inner_list.append(inner_item[1])
new_list.append(inner_list)
output of new_list = [[4, 2, 1, 3], [4, 3, 1, 2]]