Slice Operators | Catching the 2nd element in every sub list | Python

Viewed 91

I have a list of lists, each sub-list contain exactly 2 floats:

my_list = [[0, 1], [0, 1], [0, 1]]

Is there a one liner slice operation; so as to not have a for loop?

Desired Output:

> [[1], [1], [1]]

Then, I would like to merge these sub-lists as elements into one list. Outputting, as dtype list:

> [1, 1, 1]

Failed attempt:

d_[:][1]
> [[0, 1]]
1 Answers

You can use list comprehension

ans = [x[1] for x in my_list]

Full answer:

my_list = [[0, 1], [0, 1], [0, 1]]
ans = [x[1] for x in my_list]

print(ans)

>>> [1, 1, 1]
Related