You put if at the end:
[y for y in a if y not in b]
List enumerations are written in the same order as their enclosed full-sized copies, basically the above statement is translated into:
outputlist = [] for y in a: if y not in b: outputlist.append(y)
Your version tried to do this instead:
outputlist = [] if y not in b: for y in a: outputlist.append(y)
but a list comprehension must begin with at least one outer loop.
Martijn Pieters Mar 18 '13 at 10:47 2013-03-18 10:47
source share