Re: Simple - looking for a way to do an element exists check..

2008-02-23 Thread Boris Ozegovic
Paul Rubin wrote: if any(x[0]==element[0] for x in a): How come this list comprehension isn't in [] brackets? -- http://mail.python.org/mailman/listinfo/python-list

Re: Simple - looking for a way to do an element exists check..

2008-02-23 Thread TeroV
Boris Ozegovic wrote: Paul Rubin wrote: if any(x[0]==element[0] for x in a): How come this list comprehension isn't in [] brackets? It isn't list comprehension, it is generator expression http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Generator_expressions -- Tero --

Re: Simple - looking for a way to do an element exists check..

2008-02-23 Thread Boris Ozegovic
TeroV wrote: It isn't list comprehension, it is generator expression http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Generator_expressions Nice. :) -- http://mail.python.org/mailman/listinfo/python-list

Re: Simple - looking for a way to do an element exists check..

2008-02-23 Thread Paul Hankin
On Feb 22, 7:01 pm, Paul McGuire [EMAIL PROTECTED] wrote: On Feb 22, 12:54 pm, Paul Rubin http://[EMAIL PROTECTED] wrote: Paul Rubin http://[EMAIL PROTECTED] writes:     if any(x==element[0] for x in a):       a.append(element) Should say:      if any(x[0]==element[0] for x in a):

Re: Simple - looking for a way to do an element exists check..

2008-02-23 Thread thebjorn
On Feb 23, 6:18 pm, Paul Hankin [EMAIL PROTECTED] wrote: On Feb 22, 7:01 pm, Paul McGuire [EMAIL PROTECTED] wrote: On Feb 22, 12:54 pm, Paul Rubin http://[EMAIL PROTECTED] wrote: Paul Rubin http://[EMAIL PROTECTED] writes: if any(x==element[0] for x in a):

Re: Simple - looking for a way to do an element exists check..

2008-02-23 Thread Paul Rubin
thebjorn [EMAIL PROTECTED] writes: If the lists are long enough to care, either rewrite use a set-based solution if the items are hashable, or keep the elements sorted and rewrite to use the bisect module if the elements can be ordered. Well, you want a tree data structure to have fast

Re: Simple - looking for a way to do an element exists check..

2008-02-23 Thread thebjorn
On Feb 24, 2:24 am, Paul Rubin http://[EMAIL PROTECTED] wrote: thebjorn [EMAIL PROTECTED] writes: If the lists are long enough to care, either rewrite use a set-based solution if the items are hashable, or keep the elements sorted and rewrite to use the bisect module if the elements can be

Re: Simple - looking for a way to do an element exists check..

2008-02-23 Thread Marc 'BlackJack' Rintsch
On Sat, 23 Feb 2008 17:19:47 -0800, thebjorn wrote: On Feb 23, 6:18 pm, Paul Hankin [EMAIL PROTECTED] wrote: IMO Jason's solution of testing containment in a generator is better (more readable). if element[0] not in (x[0] for x in a): a.append(element) It may be more readable

Simple - looking for a way to do an element exists check..

2008-02-22 Thread rh0dium
Hi all, I have a simple list to which I want to append another tuple if element 0 is not found anywhere in the list. element = ('/smsc/chp/aztec/padlib/5VT.Cat', '/smsc/chp/aztec/padlib', '5VT.Cat', (33060)) element1 = ('/smsc/chp/aztec/padlib/5VT.Cat2', '/smsc/chp/aztec/padlib',

Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul McGuire
On Feb 22, 11:20 am, rh0dium [EMAIL PROTECTED] wrote: found = False for item in a:   if item[0] == element[0]     found = True     break if not found:   a.append(element) But this is just ugly - Is there a simpler way to interate over all items in a without using a found flag? Thanks

Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul McGuire
On Feb 22, 11:20 am, rh0dium [EMAIL PROTECTED] wrote: Hi all, I have a simple list to which I want to append another tuple if element 0 is not found anywhere in the list. element =  ('/smsc/chp/aztec/padlib/5VT.Cat',   '/smsc/chp/aztec/padlib',   '5VT.Cat', (33060)) element1 =  

Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Jason
On Feb 22, 10:20 am, rh0dium [EMAIL PROTECTED] wrote: Hi all, I have a simple list to which I want to append another tuple if element 0 is not found anywhere in the list. element = ('/smsc/chp/aztec/padlib/5VT.Cat', '/smsc/chp/aztec/padlib', '5VT.Cat', (33060)) element1 =

Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul Rubin
rh0dium [EMAIL PROTECTED] writes: found = False for item in a: if item[0] == element[0] found = True break if not found: a.append(element) But this is just ugly - Is there a simpler way to interate over all items in a without using a found flag? Untested and I'm not sure I

Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul Rubin
Paul Rubin http://[EMAIL PROTECTED] writes: if any(x==element[0] for x in a): a.append(element) Should say: if any(x[0]==element[0] for x in a): a.append(element) -- http://mail.python.org/mailman/listinfo/python-list

Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul McGuire
On Feb 22, 12:54 pm, Paul Rubin http://[EMAIL PROTECTED] wrote: Paul Rubin http://[EMAIL PROTECTED] writes:     if any(x==element[0] for x in a):       a.append(element) Should say:      if any(x[0]==element[0] for x in a):         a.append(element) I think you have this backwards.

Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul Rubin
Paul McGuire [EMAIL PROTECTED] writes: I think you have this backwards. Should be: if not any(x[0]==element[0] for x in a): a.append(element) I think you are right, it was too early for me to be reading code when I posted that ;-) --

Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul McGuire
On Feb 22, 3:38 pm, Paul Rubin http://[EMAIL PROTECTED] wrote: Paul McGuire [EMAIL PROTECTED] writes: I think you have this backwards.  Should be:      if not any(x[0]==element[0] for x in a):         a.append(element) I think you are right, it was too early for me to be reading code

Re: Simple - looking for a way to do an element exists check..

2008-02-22 Thread Paul Rubin
Paul McGuire [EMAIL PROTECTED] writes: I'm still getting used to 'any' and 'all' as new Python built-ins - but they'll do the short-circuiting as well as a for-loop-with-break. But I think a set- or dict-based solution will still surpass a list- based one for the OP. I guess I don't