Alex Savin Alex Savin - 6 months ago 8
Python Question

How to create one list with selected elements from another

I have list:

[[(5, 0.13074668991267904), (7, 0.50480936593447379), (11, 0.24881894415284611)]
[(19, 0.2984135829981458), (24, 0.31341486626464521), (29, 0.15972991970724332), (31, 0.12844163102996534)]]


I need to create a new list with second values from previous list, which have to look like:

[[0.13074668991267904, 0.50480936593447379, 0.24881894415284611)]
[0.2984135829981458, 0.31341486626464521, 0.15972991970724332), 0.12844163102996534)]]


How can I do that?

Answer

You can just iterate over the original list(s) grabbing the last element in each as one option.

new_list = [[x[-1] for x in a_list] for a_list in nested_lists]