Is local variable necessary in Python comprehensions?

list-comprehension, python, warnings

Solution

The convention when a variable is unused is to use an underscore instead:

r = [rand_foo() for _ in range(10)]

See for example: Underscore _ as variable name in Python

I believe this will suppress your PyCharm warning

Problem

In Python 3.x, I'm calling a function `rand_foo()` which returns some random stuff each time being called. I wish to store the sequence of random results into a list. I'm using the following construct: ``` r = [ rand_foo() for i in range(10) ] ``` Now my PyCharm 3.0 IDE keeps warning: `Local variable 'i' value is not used`. Is there an elegant way of removing the unnecessary variable? Indeed, in some cases, I could use `itertools.repeat()` or something like `10*[value]`, which, however, cannot be applied to my example above.

Original source

Related problems