3

I wonder if there is a way to use e.g. a list comprehension without an iteration variable if I do not need it? For example, in this sample of code:

a = [random.randrange(-10, 11) / 10 for i in range(100)]

I get a warning "Local variable 'i' value is not used". Is there any variant of the list comprehension construct without iteration variable?

jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
zegoline
  • 554
  • 1
  • 8
  • 21
  • What IDE/editor are you using? You might be able to change `i` to `_` which is the common notation for "I deliberately mean to not be using this" and your IDE might ignore it, but otherwise - it's fine. – Jon Clements May 29 '15 at 11:31

1 Answers1

4

Just discard the value of i:

a = [random.randrange(-10, 11) / 10 for _ in range(100)]

_ is considered the "last value" in Python and by convention is used as a "throw away" value.

James Mills
  • 17,799
  • 3
  • 45
  • 59