Re: Negative subscripts

2021-11-29 Thread Chris Angelico
On Tue, Nov 30, 2021 at 6:45 AM dn via Python-list wrote: > > > > On 27/11/2021 21.23, Chris Angelico wrote: > > On Sat, Nov 27, 2021 at 7:21 PM dn via Python-list > > wrote: > >> The expression list is evaluated once; it should yield an iterable > >> object. An iterator is created for the result

Re: Negative subscripts

2021-11-29 Thread dn via Python-list
On 27/11/2021 21.23, Chris Angelico wrote: > On Sat, Nov 27, 2021 at 7:21 PM dn via Python-list > wrote: >> The expression list is evaluated once; it should yield an iterable >> object. An iterator is created for the result of the expression_list. >> The suite is then executed once for each item

Re: Negative subscripts

2021-11-27 Thread Chris Angelico
On Sat, Nov 27, 2021 at 7:21 PM dn via Python-list wrote: > The expression list is evaluated once; it should yield an iterable > object. An iterator is created for the result of the expression_list. > The suite is then executed once for each item provided by the iterator, > in the order returned b

Re: Negative subscripts

2021-11-27 Thread dn via Python-list
On 27/11/2021 19.11, Frank Millman wrote: > On 2021-11-26 11:24 PM, dn via Python-list wrote: >> On 26/11/2021 22.17, Frank Millman wrote: >>> 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 proce

Re: Negative subscripts

2021-11-26 Thread Frank Millman
On 2021-11-26 11:24 PM, dn via Python-list wrote: On 26/11/2021 22.17, Frank Millman wrote: 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

Re: Negative subscripts

2021-11-26 Thread Greg Ewing
On 2021-11-26 11:17 AM, Frank Millman wrote: Are there any other techniques anyone can suggest, or is the only alternative to use if...then...else to cater for y = 0? x[:-y or None] Seems to work: >>> l ['a', 'b', 'c', 'd', 'e'] >>> def f(x): return l[:-x or None] ... >>> f(3) ['a', 'b'] >>>

Re: Negative subscripts

2021-11-26 Thread Edmondo Giovannozzi
Il giorno venerdì 26 novembre 2021 alle 10:23:46 UTC+1 Frank Millman ha scritto: > 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

Re: Negative subscripts

2021-11-26 Thread Pieter van Oostrum
Frank Millman writes: > 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

Re: Negative subscripts

2021-11-26 Thread dn via Python-list
On 26/11/2021 22.17, Frank Millman wrote: > 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. ... > But in my actual p

Re: Negative subscripts

2021-11-26 Thread Frank Millman
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

Re: Negative subscripts

2021-11-26 Thread Chris Angelico
On Fri, Nov 26, 2021 at 10:11 PM Rob Cliffe via Python-list wrote: > or, perhaps simplest, you could do > > for item in x[:-y or None]: # a value of None for a slice argument means > "don't slice here" > [do stuff] > This is the one I'd recommend. If you're negating a slice like this, just a

Re: Negative subscripts

2021-11-26 Thread Rob Cliffe via Python-list
You could evaluate y separately: yval = for item in x[:-yval] if yval else x:     [do stuff] or you could do it using the walrus operator: for item in x[:-yval] if (yval := ) else x:     [do stuff] or, perhaps simplest, you could do for item in x[:-y or None]: # a value of None for a slice a

Re: Negative subscripts

2021-11-26 Thread Antoon Pardon
Op 26/11/2021 om 10:17 schreef Frank Millman: 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

Negative subscripts

2021-11-26 Thread Frank Millman
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