Extracting the a value from a tuple when the other values are unused

Both are acceptable, and I've seen both in production code. I think the choice upon the context, the intent, and the local style.

There is also a third option where the code describes the unused value:

_real, imaginary = foo

I use all three within my code depending upon which is clearest:

_, x = foo

  • When the tuple is small.
  • When there are few discarded values.
  • When there are few discarded values relative to the number of extracted values.
  • The reader probably knows the tuple's composition, and the composition of the whole tuple is important.
  • When it's customary to think about the structure of the tuple as a single unit

x=foo[i]

  • When the tuple is large.
  • When there are many discarded values for some value of many.
  • When the reader probably knows the tuple's composition.
  • The rest of the tuple's values are completely and utterly irrelevant & offer no useful information for the reader.
  • When it's customary to think about the structure as a sequence.
  • When the tuple has uniform composition.
  • When it's customary to use an index for the datatype.
  • When the index is keyed in a loop.
  • When the reader's attention should be drawn to the index's value.

_real, imaginary = foo

  • When the tuple is small.
  • When there are few discarded values.
  • When the reader probably doesn't know the tuple's composition.
  • When naming the discarded value gives the reader insight. (You can guess from this one line that foo is a complex number.)

I've been using _ for over a decade. It is much more readable, especially when extracting more than one value:

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

Even with only one variable, the other way forces humans readers to count if they want to truly understand the code, and more likely their eyes are just going to pass over it.

With the underscores, you can leverage the human brain's pattern matching abilities a little better. Probably a small thing, but every little bit helps when you're debugging. :)


I think the usual way of doing it

x=foo[index]

Using _ is less common, and I think also discouraged. Using _ is also unwieldy when you need only a few elements out of a long tuple/list. Slicing also comes handy when you are only choosing a contiguous subsequence.

But at the end of the day I think it is just a matter of subjective preference. Use whatever that looks more readable to you and your team.


If this is a return value from a function or method, another alternative is to write a wrapper function (or subclass the class and add a method) that returns just the item you're interested in, and calling that instead.

Tags:

Python