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 list comprehension to get max() of each list column

I have following list of list:

lst = [[1,3], [3,4], [2,7], [6,5]]

How do I create "oneliner" to get list of max() of each "column"?

so for the example above the resulting list should be following: [6,7], where 6 is the maximum of "column 0" and 7 is the maximum of "column 1".

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

>Solution :

You can use zip which splits the pairs into two separate tuples:

>>> lst = [[1,3], [3,4], [2,7], [6,5]]

>>> list(zip(*lst))
[(1, 3, 2, 6), (3, 4, 7, 5)]

So, overall:

>>> [max(v) for v in zip(*lst)]
[6, 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