How can I get around declaring an unused variable in a for loop?

If I have a list comprehension (for example), for example:

['' for x in myList] 

Effectively creating a new list with an empty string for each item in the list, I never use x . Is there a cleaner way to write this, so I don't need to declare an unused variable x ?

+67
python list-comprehension
Mar 29 '11 at 18:26
source share
8 answers

_ is the standard placeholder for ignored elements in the for-loop and tuple assignment, for example

 ['' for _ in myList] [a+d for a, _, _, d, _ in fiveTuples] 

By the way, your list can be written without understanding the list (assuming that you want to make a list of immutable members, such as strings, integers, etc.).

 [''] * len(myList) 
+108
Mar 29 '11 at 18:27
source share
— -

No. As Zen says: Special cases are not complex enough to break the rules. A special case is cycles that do not use elements of a repeating thing, and the rule is that there is a “target” for unpacking.

However, you can use _ as the name of a variable, which is usually understood as “intentionally unused” (even PyLint, etc. knows and respects this).

+17
Mar 29 '11 at 18:28
source share

It turns out using dummy* (the start word is dummy) since the variable name does the same trick as _ . _ is a well-known standard, and it would be better to use meaningful variable names. So you can use dummy , dummy1 , dummy_anything . When using these variable names, PyLint will not complain.

+11
Mar 12 '15 at 19:43
source share

If you need to specify your arguments (in the case, for example, when writing mocks that do not use certain arguments referenced by the name), you can add this shortcut method:

 def UnusedArgument(_): pass 

and then use it like this:

 def SomeMethod(name_should_remain): UnusedArgument(name_should_remain) 
+1
May 28 '13 at 22:31
source share

Generator objects do not actually use variables. So something like

 list(('' for x in myList)) 

gotta do the trick. Note that x is not defined as a variable outside the understanding of the generator.

0
Mar 29 '11 at 19:36
source share

You can also add _ to the variable name if you prefer to give the variable a human-readable name. For example, you can use _foo , _foo1 , _anything and PyLint will not complain. In a for loop, it will look like this:

 for _something in range(10): do_something_else() 

edit: add example

0
May 13 '19 at 14:14
source share

Add the following comment after the for loop on the same line:

pylint: disable = unused-variable

 for i in range(100): #pylint: disable=unused-variable 
0
Jul 24 '19 at 22:41
source share

Comment How can I get around the declaration of an unused variable in a for loop? (Ran out of comment size)

Python maintains the same reference for the created object. (regardless of variability) e.g.

 In [1]: i = 1 In [2]: j = 1 In [3]: id(i) Out[3]: 142671248 In [4]: id(j) Out[4]: 142671248 

You can see both i and j, refer to the same object in memory. What happens when we change the value of one immutable variable.

 In [5]: j = j+1 In [6]: id(i) Out[6]: 142671248 In [7]: id(j) Out[7]: 142671236 

you can see, j now starts to indicate the new location (where 2 is stored), and I still point to the place where 1. is stored. Estimating,

 j = j+1 

The value is selected from 142671248, calculated (if it is not already cached) and placed in a new location 142671236. j is done to indicate a new location. Simply put, a new copy made each time an immutable variable changes.

Variability

In this regard, interchangeable objects are not much different from each other. When the value is specified

 In [16]: a = [] In [17]: b = a In [18]: id(a) Out[18]: 3071546412L In [19]: id(b) Out[19]: 3071546412L 

Both a and b point to the same memory location.

 In [20]: a.append(5) 

Changed the memory location indicated by a.

 In [21]: a Out[21]: [5] In [22]: b Out[22]: [5] In [23]: id(a) Out[23]: 3071546412L In [24]: id(b) Out[24]: 3071546412L 

Both a and b still point to the same memory location. In other words, mutable variables act in the same memory location that the variable points to, instead of copying the value indicated by the variable, as in the immutable case of a variable.

-one
Mar 30 '11 at 9:49
source share



All Articles