Negative subscripts

Frank Millman frank at chagford.com
Fri Nov 26 08:40:09 EST 2021


On 2021-11-26 11:17 AM, Frank Millman wrote:
> 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 for all the replies. A selection of neat ideas for me to choose from.

Much appreciated.

Frank



More information about the Python-list mailing list