value is in list?

Christian Heimes lists at cheimes.de
Thu Jun 12 20:13:55 EDT 2008


David Hláčik wrote:
> Hello ,
> following scenario
> 
> list_current = [ "welcome", "search", "done", "result"]
> list_ldap = [ "welcome", "hello"]
> 
> result:
> 
> list_toadd = [ "hello"]
> 
> by words said , i want to check if list item from list_ldap exists in
> list_current if not i want to add it to list_toadd.

Steven's example works but it won't scale for large lists. Sets are much
faster if you don't require an ordered result.

>>> list_current = [ "welcome", "search", "done", "result"]
>>> list_ldap = [ "welcome", "hello"]
>>> diff = set(list_ldap).difference(set(list_current))
>>> diff
set(['hello'])
>>> list_toadd = list(diff)
>>> list_toadd
['hello']

Christian




More information about the Python-list mailing list