+ 2

Beginner's question about slicing

I wonder if I'll ever need it, but... somelist[-3:-1] will give me a slice without the last element. Is there a way to get the last element while still counting from the back? 0 obviously can not work.

16th Jun 2018, 10:50 AM
HonFu
HonFu - avatar
4 Réponses
+ 6
Yeah, Jan Markus ' version is the standard. I'm nowhere near familiar with list slicing. https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation
16th Jun 2018, 11:20 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
somelist[-1:-4:-1]
16th Jun 2018, 11:15 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
I'm aware that there are ways to circumvent the issue, but just out of interest: is there a way to get the index of the position rightside of - 1 directly?
16th Jun 2018, 11:20 AM
HonFu
HonFu - avatar
+ 2
Haha, how about this? ;-) [1,2,3][-1:-4:-1][::-1] Not exactly as concise as I'd like it to be though... :D Or do you find it concise, Denise Roßberg?
16th Jun 2018, 3:29 PM
HonFu
HonFu - avatar