I have created a list size 12, ex: V[0 1 2 3 4 5 6 7 8 9 10 11]
How do I return only the last 10 digits [2:11]
? Also what if the list had n variables, I tried V[2,:]
, but I get TypeError: list indices must be integers or slices, not tuple
.
Asked
Active
Viewed 44 times
0
-
The last 10 digits is `V[-10:]`. I'm not sure how `n` is supposed to be used here, but you can do `V[2:]` if that's what you mean. You can read the basics on slicing in the std tutorial https://docs.python.org/3.6/tutorial/introduction.html. – tdelaney Jan 31 '17 at 00:03
1 Answers
2
If you want to get the last x
elements of a list, you need to use a negative index in your slice.
>>> numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> numbers[-5:]
[5, 6, 7, 8, 9]
As for the error you mentioned, it looks like you have a stray comma after the 2, which makes 2 the first element in a tuple. Take out the comma and it should be a valid slice.

Dan
- 557
- 5
- 12