Raw parts when unpacking a tuple / list

Python is all about writing beautiful code. So, I did pylint to check the "prettiness" of my code when I delve into something:

Unused variable 'myvar1'

From this part of my code:

 for myvar1, myvar2 in mylist: # Do stuff just using myvar2 

mylist is a list of tuples, so I expand the tuples into two variables ( myvar1 and myvar2 ). I define these two variables to expand the second, because I do not need the other.

So, here is my question: Is there a way to tell the interpreter to expand the tuple, but not perceive the first part (for example). In some other languages, you can do something like:

 for _, myvar in mylist: # Do stuff with myvar 

or

 for *, myvar in mylist: # Do stuff with myvar 

This means: I donโ€™t care about the first part of the tuple, I just need the second.

NOTE: I know this may be an option for what I ask:

 for mytuple in mylist: # Do stuff with mytuple[1] 

But it is much less readable.

+4
source share
6 answers

In addition to @RaymondHettinger answer: Pylint also does not complain about unused variables if their names begin with a single underscore. This means that you can use:

 for _myvar1, myvar2 in mylist: 

get the best of both worlds:

  • no Pylint warning,
  • and record structure information

This also works for prototypes of functions and methods and avoids warnings about unused parameters, which you can often get when getting a base class in an OO environment.

+5
source

Have you tried this?

 for _, myvar in mylist: #Do stuff 

works great in Python and is relatively idiomatic.

+3
source

I am writing for _, myvar2 in mylist , when I want to emphasize that only myvar2 is used.

And I write for myvar1, myvar2 in mylist when I want to remind the reader (usually me) what a record structure is.

The name _ is just a naming convention for the throw value. The CPython interpreter does the assignment of a variable for it in the same way as with any other variable name (fortunately, * STORE_FAST * is a very cheap operation). In contrast, the PyPy interpreter identifies the unused variable assignment as dead code, so you get free optimization no matter how you write it.

If you are interested in how CPython interprets your code, dis module can provide useful information:

 >>> from dis import dis >>> def f(lot): for _, var2 in lot: print var2 >>> dis(f) 2 0 SETUP_LOOP 25 (to 28) 3 LOAD_FAST 0 (lot) 6 GET_ITER >> 7 FOR_ITER 17 (to 27) 10 UNPACK_SEQUENCE 2 13 STORE_FAST 1 (_) 16 STORE_FAST 2 (var2) 3 19 LOAD_FAST 2 (var2) 22 PRINT_ITEM 23 PRINT_NEWLINE 24 JUMP_ABSOLUTE 7 >> 27 POP_BLOCK >> 28 LOAD_CONST 0 (None) 31 RETURN_VALUE 

As other posters noted, warnings from pylint can sometimes be blank. If you prefer the short variable name in your code, just ignore the pylint complaint. As Francis Avila noted, pilint should not complain about _ in this context.

+3
source

I suppose you could do this:

 for myvar in (t[1] for t in mylist): pass 

But honestly, I think that you should just ignore the pylint warning in this case - it is pretty enough and will not cause any confusion (which is why you want beauty in the first place).

+2
source

I would say that Python is all about writing readable code - any โ€œbeautyโ€ is just a side effect.

The first element of the tuple can be excluded as follows:

 for myvar2 in zip(*mylist)[1]: # Do stuff with myvar2 

But I'm not sure what I really recommend. Personally, I would just use:

 for myvar1, myvar2 in mylist: # Do stuff with myvar2 

... and ignore pylint.

+2
source
 tu = [(12,'sea'),(478,'badada'),(789,'zut')] for x,x in tu: print x 

result

 sea badada zut 

-1
source

Source: https://habr.com/ru/post/1384398/


All Articles