List comprehension in if clause of another list comprehension

Vedran Furac( vedranf at vedranf.mine.nu
Fri Dec 19 12:16:44 CET 2008


Hi!

In [69]: a = 'a b c'
In [70]: b = 'a b, c d'

In [74]: [i for i in a.split() if i not in b.split()]
Out[74]: ['b']

Everything ok.

In [77]: b.split() == [i for i in b.split()]
Out[77]: True

As expected. Now, put this in the first list comprehension:

In [80]: [i for i in a.split() if i not in [i for i in b.split()] ]
Out[80]: ['d']

Hmmmm... why is that?

Regards!



More information about the Python-list mailing list