[Tutor] Comparing lists

Ashish asis@graffiti.net
Tue, 09 Jul 2002 20:50:18 +0545


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....)
> 


may be
common = []
for i in lista:
     if i in listb:
         common.append(i)



-- 
Ashish Shrestha
Gha 2-482, Balajutar, Kathmandu, Nepal
Phone: 977-1-350593.