views:

176

answers:

5

In python, when you have a list of tuples, you can iterate over them. For example when you have 3d points then:

for x,y,z in points:
    pass
    # do something with x y or z

What if you only want to use the first variable, or the first and the third. Is there any skipping symbol in python?

+4  A: 

Is something preventing you from not touching variables that you're not interested in? There is a conventional use of underscore in Python to indicate variable that you're not interested. E.g.:

for x, _,_ in points:
    print(x)

You need to understand that this is just a convention and has no bearing on performance.

SilentGhost
This was an example. Normally I can have 4 variables in a tuple, and I'm too lazy to invent good new variable names for them.
Peter Smit
@Peter: You don't have to have *good* names for variables that you're not going to use.
SilentGhost
In fact `_` is probably the least descriptive variable name you could possibly come up with ;-) Or it would be, if it weren't for this convention.
David Zaslavsky
Ok, another reason. Besides inventing a good name for something I don't use, my IDE will warn me for unused local variables if I just make something up there. Underscore works well!
Peter Smit
+6  A: 

Yes, the underscore:

>>> a=(1,2,3,4)
>>> b,_,_,c = a
>>> b,c
(1, 4)

This is not exactly 'skipping', just a convention. Underscore variable still gets the value assigned:

>>> _
3
unbeli
The information regarding `_` is interesting. So `a, a, a = (1, 2, 3)` actually works ...
Dario
@Dario I would not recommend that, as it's hard to read and understand which value you are expecting in a
unbeli
@unbeli: Yes, I wouldn't even *use* it for that reason, but I thought Python would therefore disallow it.
Dario
@Dario: disallowing "it" (you haven't define "it") would take core developer effort that almost everybody would agree would be better spent elsewhere ... love to see your proposed changes to the Python grammar.
John Machin
+5  A: 

A common way to do this is to use underscores for the unused variables:

for x, _, z in points:
    # use x and z

This doesn't actually do anything different from what you wrote. The underscore is a normal variable like any other. But this shows people reading your code that you don't intend to use the variable.

It is not advisable to do this in the interactive prompt as _ has a special meaning there: the value of the last run statement/expression.

Mark Byers
+3  A: 

While this is not as slick as you're asking for, perhaps this is most legible for your intentions of giving meaningful names only to the tuple indices you care about:

for each in points:
    x = each[0]
    # do something with x
stw_dev
A: 

In Python 3.1 you can use an asterisk in front of an identifier on the left side of a tuple assignment and it will suck up whatever is left over. This construct will handle a variable number of tuple items. Like this:

>>> tpl = 1,2,3,4,5
>>> a, *b = tpl
>>> a
1
>>> b
>>> (2, 3, 4, 5)

Or in various orders and combinations:

>>> a, *b, c = tpl
>>> a
1
>>> b
(2, 3, 4)
>>> c
5

So, for the case you asked about, where you're only interested in the first item, use *_ to suck up and discard the remaining items you don't care about:

>>> a, *_ = tpl
>>> a
1
Don O'Donnell