01 May 2022

How to pick up a single column from a 2-dimensional list in Python?

https://stackoverflow.com/questions/30062429/how-to-get-every-first-element-in-2-dimensional-list

a = [[4.0, 4, 4.0], [3.0, 3, 3.6], [3.5, 6, 4.8]]

You can get the index [0] from each element in a list comprehension


>>> [i[0] for i in a]
[4.0, 3.0, 3.5]

No comments:

Post a Comment