Question

I have data in the form of a list of lists. I wanted to do something like (data[1::100])[3], where I access the first 100 elements, and then just the third column of all those elements. In tutorials on Kaggle, I had seen things like data[1::100,3], but Python says:

TypeError: list indices must be integers, not tuple.

Is there an easy way to do this? I could just use a loop or something, but this will come up a lot so it would be nice to have some shorthand.

Was it helpful?

Solution

In a list of lists, you can do:

values = [l[3] for l in data[:100]]

to achieve the same thing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top