Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python create a sublist of list from a list

Here is my question: I have a list in python that looks like this:

list = [["a", "b", [1,2]], ["a", "C", [4,1,3]], ["e", "f", [1,7]]]

And I would like to get a sublist like this:

sublist = [[1,2], [4,1,3], [1,7]]

I would like to know if there is a quick way to make this transition,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you in advance for your help 🙂

>Solution :

Don’t use list as a name for a list as it clashes with the built-in name. Use this:

l = [["a", "b", [1,2]], ["a", "C", [4,1,3]], ["e", "f", [1,7]]]
sublist = [i[-1] for i in l]

Output:

[[1, 2], [4, 1, 3], [1, 7]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading