Possible duplicate:
Is Pythonic using lists for side effects only?
Sometimes in a script file I write something like [foo(x) for x in (1,2,3)]. I really don't care about the return value (if any), I just prefer shorthand rather than using
for x in (1,2,3):
foo(x)
But in this sense, I create a list object that doesn’t actually go anywhere (I think it gets garbage because no one refers to it?)
>>> L = list('SxPyAMz')
>>> [L.remove(c) for c in ('x', 'y', 'z')]
[None, None, None]
>>> print L
['S', 'P', 'A', 'M']
My question is: is this bad practice, or is it completely harmless to the object so that the whistle is imperceptible? If this is bad, is there a better 1-liner for these shortcuts?
source
share