Re: Checking whether list element exists

2007-04-07 Thread Rehceb Rotkiv
Thanks for your many helpful tips! Rehceb Rotkiv -- http://mail.python.org/mailman/listinfo/python-list

Re: Checking whether list element exists

2007-04-07 Thread Terry Reedy
"Rehceb Rotkiv" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] |I want to check whether, for example, the element myList[-3] exists. So | far I did it like this: | | index = -3 | if len(myList) >= abs(index): | print myList[index] # Note that tabs gets lost in some newsreaders. Space

Re: Checking whether list element exists

2007-04-07 Thread Gary Herron
Rehceb Rotkiv wrote: > I want to check whether, for example, the element myList[-3] exists. So > far I did it like this: > > index = -3 > if len(myList) >= abs(index): > print myList[index] > > Another idea I had was to (ab-?)use the try...except structure: > > index = -3 > try: > prin

Re: Checking whether list element exists

2007-04-07 Thread [EMAIL PROTECTED]
On Apr 7, 10:52�am, Rehceb Rotkiv <[EMAIL PROTECTED]> wrote: > > In general case it won't work, because lists accept negative indexes: > >http://docs.python.org/lib/typesseq.html, 3rd note. > > Yes, I know! I _want_ the "3rd last list element", i.e. list[-3]. But it > may be that the list does not

Re: Checking whether list element exists

2007-04-07 Thread [EMAIL PROTECTED]
On Apr 7, 10:37�am, Wojciech Mula <[EMAIL PROTECTED]> wrote: > Rehceb Rotkiv wrote: > > I want to check whether, for example, the element myList[-3] exists. So > > far I did it like this: > > > index = -3 > > if len(myList) >= abs(index): > > � �print myList[index] > > IMHO it is good way. > > > An

Re: Checking whether list element exists

2007-04-07 Thread Rehceb Rotkiv
> In general case it won't work, because lists accept negative indexes: > http://docs.python.org/lib/typesseq.html, 3rd note. Yes, I know! I _want_ the "3rd last list element", i.e. list[-3]. But it may be that the list does not have 3 elements. In this case, list[-3] will throw an error, cf.:

Re: Checking whether list element exists

2007-04-07 Thread Wojciech Muła
Rehceb Rotkiv wrote: > I want to check whether, for example, the element myList[-3] exists. So > far I did it like this: > > index = -3 > if len(myList) >= abs(index): > print myList[index] IMHO it is good way. > Another idea I had was to (ab-?)use the try...except structure: > > index =

Checking whether list element exists

2007-04-07 Thread Rehceb Rotkiv
I want to check whether, for example, the element myList[-3] exists. So far I did it like this: index = -3 if len(myList) >= abs(index): print myList[index] Another idea I had was to (ab-?)use the try...except structure: index = -3 try: print myList[index] except: print