[Tutor] Comparing more than 2 lists

Alan Gauld alan.gauld at btinternet.com
Fri Jan 25 00:14:28 CET 2008

"Fiyawerx" <fiyawerx at gmail.com> wrote 

> list1 = ['one', 'two', 'three']
> list2 = ['one', 'two', 'four', 'five']
> list3 = ['two', 'three', 'six', 'seven']
> list4 = ['three', 'five', 'six']
> I need to be able to get along the lines of output:
> Element 'one' contained in list1 and list2
> Element 'two' contained in list1 and list2 and list3
> ...
> Element 'five' contained in list2 and list4
> etc.. and I can't quite figure out how to go about it

There are lots of approaches, one of the easest is to 
use a dictionary keyed by the elements of the lists 
and the values the identity of the list in which it 

d = {}
for n, lst in enumerate([list1,list2,list3,list4[):
   for item in lst:

Gets close


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list