Retrieving a value from a tuple when other values ​​are not used

I have a foo tuple that contains something that doesn't bother me, and something I do.

 foo = (something_i_dont_need, something_i_need) 

Is it more correct to use

 _, x = foo 

or

 x = foo[1] 

The only thing I can imagine is a different behavior if foo has no length two. I believe this is quite case-specific, but is one of them a de facto pythonic way of doing something?

+6
python
source share
5 answers

I think the usual way to do this

 x=foo[index] 

Using _ is less common, and I think this is also not recommended. Using _ also cumbersome when you only need a few items from a long tuple / list . Slicing is also convenient when you select only a continuous subsequence.

But at the end of the day, I think it's just a matter of subjective preference. Use what looks more readable to you and your team.

+5
source share

I have been using _ for over ten years now. This is much more readable, especially when extracting more than one value:

  _, _, name, _, _, city, _ = whatever 

Even with one variable, another way makes readers think if they really want to understand the code, and most likely their eyes will simply go through it.

With underscores, you can slightly improve the ability to match the picture of the human brain. Probably a little thing, but every bit helps when you are debugging. :)

+8
source share

Both are acceptable, and I saw both in production code. I think the choice is according to context, intention and local style.

There is also a third option, in which the code describes an unused value:

_real, imaginary = foo

I use all three in my code, depending on which is clearer:

_, x = foo

  • When the tuple is small.
  • When there are several discarded values.
  • When there are several discarded values ​​relative to the number of values ​​retrieved.
  • The reader probably knows the composition of the tuple, and the composition of the entire tuple is important.
  • When it is customary to think about the structure of a tuple as a separate element

x=foo[i]

  • When the motorcade is big.
  • When there are many discarded values ​​for some value of many.
  • When the reader probably knows the composition of the tuple.
  • The remaining values ​​of the tuples are completely and completely irrelevant and do not provide useful information to the reader.
  • When it is customary to think of structure as a sequence.
  • If the tuple has a uniform composition.
  • When it is customary to use an index for a data type.
  • When an index is entered into a loop.
  • When the attention of the reader should be paid to the index value.

_real, imaginary = foo

  • When the tuple is small.
  • When there are several discarded values.
  • When the reader probably does not know the composition of the tuple.
  • The name of the discarded value gives an understanding of the reader. (You can guess from this one line that foo is a complex number.)
+1
source share

If it is a return value from a function or method, another alternative is to write a wrapper function (or subclass of the class and adding a method) that returns only the element you are interested in and calls it instead.

+1
source share

1st case: foo requires two variables before unpack (the length of the tuple is 2). _ well.

Second case: gives you index value (slicing)

0
source share

All Articles