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

Paul Rubin http
Fri Feb 22 13:50:04 EST 2008


rh0dium <steven.klass at gmail.com> 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 understand the question completely, but
try:

    if any(x==element[0] for x in a): 
      a.append(element)



More information about the Python-list mailing list