what does 'for _ in range()' mean?

Dave Benjamin ramen at lackingtalent.com
Thu Jul 29 20:30:39 EDT 2004


In article <41076E3E.233CE3F1 at alcyone.com>, Erik Max Francis wrote:
> Note also there's a noticeable difference between the anonymous variable
> in Prolog and the use of _ in Python; in Prolog, the anonymous variable
> can be used multiple times in the same expression and there is no need
> for the variable to represent the same thing.  In
> 
> 	middle(X) :- inOrder(_, X, _).

In Python, it's possible to use _ multiple times in the same expression,
too. I guess there's no obvious reason why this shouldn't work, but I
discovered the following tuple-unpacking idiom recently:

>>> for _, val, _ in ((1, 2, 3), (4, 5, 6), (7, 8, 9)):
...     print val
...     
2
5
8

-- 
  .:[ dave benjamin: ramen/[sp00] -:- spoomusic.com -:- ramenfest.com ]:.

"When the country is confused and in chaos, information scientists appear."
Librarian's Lao Tzu: http://www.geocities.com/onelibrarian.geo/lao_tzu.html



More information about the Python-list mailing list