I have several sampling functions that need to be called for a certain amount of times as in the following instruction:
samples = [do_sampling() for _unused in range(n_samples)]
I wonder, is there a more compact way to express that statement, especially by avoiding the _unused
variable?
There's not a better way to do this that I'm aware of. The convention is to do this:
samples = [do_sampling() for _ in range(n_samples)]
If you're using Python 2.x and not Python 3.x, you can gain a performance improvement by using a generator-version of range, xrange()
, rather than range()
:
samples = [do_sampling() for _ in xrange(n_samples)]
The _
variable is typically used as Pythonic notation for a discarded, unused variable.