[Tutor] Comparing lists

Lloyd Kvam pythontutor@venix.com
Tue, 09 Jul 2002 11:03:00 -0400


This is untested:

def inBoth(alist, blist):
	return [a for a in alist if a in blist]

This can be slow for large lists.  It will be empty if there are no entries
in common.

Terje Johan Abrahamsen wrote:

> I have two lists of different length. Actually each list can differ in 
> lenght every time. For example lista = [12, 34, 56] & listb = [14, 16, 
> 34, 81]. What I now need to do is to check if the lists have one or more 
> common numbers. In this case 34 exists in each list. Then I would like 
> the function to somehow indicate this. Does it exist a simple way to do 
> this? I have made a somehow functioning function that does this, but it 
> is probably about 50-60 lines. (If lista[0] == listb[0], if lista[1] == 
> listb[0] and so forth....)
> 
> Any suggestions?
> 
> Thanks,
> Terje
> 
> 
> 
> _________________________________________________________________
> Join the world's largest e-mail service with MSN Hotmail. 
> http://www.hotmail.com
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice: 
603-443-6155
fax: 
801-459-9582