A "for" with "list" question.

Graham Ashton graham at effectif.com
Sat Aug 31 23:50:10 CEST 2002


On Sat, 31 Aug 2002 14:30:51 +0000, Mauro wrote:

> Hy to all,
> 
> I've got 2 lists and I want to print print only the common, itens in
> both lists and the not common after.

This solution may not be the quickest (I don't know and haven't really
thought it through), but this is syntactically pretty nice:

>>> a = [0, 1, 2]
>>> b = [1, 2, 3]
>>> [item for item in a if item in b]
[1, 2]

That third line is a list comprehension.

--
graham



More information about the Python-list mailing list