Ignore part of a python tuple

Jim Jeffries picture Jim Jeffries · Mar 2, 2012 · Viewed 29.3k times · Source

If I have a tuple such as (1,2,3,4) and I want to assign 1 and 3 to variables a and b I could obviously say

myTuple = (1,2,3)
a = my_tuple[0]
b = myTuple[2]

Or something like

(a,_,b,_) = myTuple

Is there a way I could unpack the values, but ignore one or more of them of them?

Answer

NPE picture NPE · Mar 2, 2012

I personally would write:

a, _, b = myTuple

This is a pretty common idiom, so it's widely understood. I find the syntax crystal clear.