Your issue appears to be with understanding how list comprehensions work, and when you might want to use one.
A list comprehension goes through every item in an list, applies a function to it, and may or may not filter out other elements. For instance, if I had the following list:
digits = [1, 2, 3, 4, 5, 6, 7]
And I used the following list comprehension:
squares = [i * i for i in digits]
I would get: [1, 4, 9, 16, 25, 36, 49]
I could also do something like this:
even_squares = [i * i for i in digits if i % 2 == 0]
Which would give me: [4, 16, 36]
Now let's talk about your list comprehensions in particular. You wrote [x[401] for x in dataset]
, which, in English, reads as "a list containing the 401st element of each item in the list called dataset".
Now, in all likelihood, there aren't more than 402 items in each line of your dataset, meaning that, when you try to access the 401st element of each, you get an error.
It sounds like you're just trying to get all the elements in dataset
excluding the last one. To do that, you can use python's slice notation. If you write dataset[:-1]
, you'll get all items in the dataset other than the last one. Similarly, if you wrote dataset[:-2]
, you'd get all items except for the last two, and so on. The same works if you want to cut off the front of the list: dataset[1:-1]
will give you all items in the list excluding the 0th and last items.
Edit:
Now that I see the new comments on your post, it's clear that you are trying to get the first 401 elements of each item in the dataset. Unfortunately, because we don't know anything about your dataset, it's impossible to say what exactly the issue is.