0

In Python, to create an array containing x random numbers, one can do:

import random
x = 10
[random.rand() for i in range(x)]

A variable i which is never being used, so this generates the warning

Unused variable: i

in PyDev - and I assume in other IDE's as well (did not check).

Is there another Pythonesque way to achieve the same result without a warning?

parvus
  • 5,706
  • 6
  • 36
  • 62
  • 2
    The convention is to use `_` or `__` as the 'ignored' variable. – Martijn Pieters Jun 23 '16 at 09:23
  • 1
    `[random.rand() for _ in range(x)]` – Moses Koledoye Jun 23 '16 at 09:24
  • Or sometimes, the more explicit `dummy`. Either way, all IDEs allow customization of which variable name(s) are allowed to be unused. Especially as `_` is also the common alias for `gettext`. – spectras Jun 23 '16 at 09:26
  • @MartijnPieters, how is it a duplicate? The questions are different for sure though answers are similar. – Vadim Pushtaev Jun 23 '16 at 09:33
  • 1
    @VadimPushtaev: the goal of closing as a duplicate is to provide an answer to this question. The questions involved don't have to be perfectly the same. – Martijn Pieters Jun 23 '16 at 09:34
  • @MartijnPieters, yeah, but "ignoring warning in PyDev" is not even nearly the same as "what _ means in Python". If people have a problem with PyDev, they don't google "_ in Python", they google "warning in PyDev". – Vadim Pushtaev Jun 23 '16 at 09:40
  • @VadimPushtaev: yes, and this post will thus redirect to that one. PyCharm recognises `_` as a placeholder. – Martijn Pieters Jun 23 '16 at 09:45

0 Answers0