Negative subscripts

Frank Millman frank at chagford.com
Fri Nov 26 04:17:18 EST 2021


Hi all

In my program I have a for-loop like this -

 >>> for item in x[:-y]:
...    [do stuff]

'y' may or may not be 0. If it is 0 I want to process the entire list 
'x', but of course -0 equals 0, so it returns an empty list.

In theory I can say

 >>> for item in x[:-y] if y else x:
...    [do stuff]

But in my actual program, both x and y are fairly long expressions, so 
the result is pretty ugly.

Are there any other techniques anyone can suggest, or is the only 
alternative to use if...then...else to cater for y = 0?

Thanks

Frank Millman


More information about the Python-list mailing list