set.remove
is an in-place operation. This means that it does not return anything (well, it returns None
); and bool(None)
is False
.
So your list comprehension is effectively this:
answer = []
for item in seq:
if item in uniq and not uniq.remove(item):
answer.append(item)
and since python does short circuiting of conditionals (as others have pointed out), this is effectively:
answer = []
for item in seq:
if item in uniq:
if not uniq.remove(item):
answer.append(item)
Of course, since unique.remove(item)
returns None
(the bool
of which is False
), either both conditions are evaluated or neither.
The reason that the second condition exists is to remove item
from uniq
. This way, if/when you encounter item
again (as a duplicate in seq
), it will not be found in uniq
because it was deleted from uniq
the last time it was found there.
Now, keep in mind, that this is fairly dangerous as conditions that modify variables are considered bad style (imagine debugging such a conditional when you aren't fully familiar with what it does). Conditionals should really not modify the variables they check. As such, they should only read the variables, not write to them as well.
Hope this helps