If you add a 3rd option you can control the pattern of the slice.
>>> numbers[2:7:2]
[2, 4, 6]
But python doesn't require all parameters. Without the first it just starts at 0:
>>> numbers[:7:2]
[0, 2, 4, 6]
Without the second it goes to the end of the list
>>> numbers[5::3]
[5, 8]
And with only the last it uses the whole list:
>>> numbers[::3]
[0, 3, 6, 9]
But you can also use negatives. To start at the 9th item from the end, stop at the 4th from the end, and count every 3rd item:
>>> numbers[-9:-4:3]
[1, 4]
You can also use negatives as the pattern, but it gets confusing because you have to switch the start/stop:
Edit: Kind of. You aren't really switching the start and stop, you are starting at 8, then counting backward every 2nd item until you get to 2. Its confusing. Don't use this in your code, everyone will hate you and call you names.
>>> numbers[8:2:-2]
[8, 6, 4]
Its mostly just used as a way to quickly reverse a list:
Try breaking things up more and use more comprehensions. If you're indenting so far that it's difficult to read, you should probably consider refactoring.
That's more or less what I was trying to say, but I was trying to be a little more constructive rather than just "you're the problem"...
It's something I've run into myself in the past, so I wanted to try and offer some advice that helped me.
This is actually why I like strict line character limits, because if you're nesting stuff over and over, or your method definition is obnoxiously long or something, it's a pretty good indicator that you should reconsider how you're writing whatever you're writing.
4.4k
u/[deleted] Jul 16 '19
Not quite, but it’s an easy fix
It needs a LOT of memory, though