getting an index in a for loop
Erik Max Francis
max at alcyone.com
Fri Jan 31 23:33:26 EST 2003
Paul Rubin wrote:
> Do they really??? I would have hoped that enumerate would build an
> iterator, something like
>
> def enumerate(seq):
> i = 0
> for s in seq:
> yield s
well, yield i, s, yeah.
> i += 1
>
> If enumerate really does build the whole list, either I'm missing
> something basic or else there was a big blunder.
Err, you're right, it does:
>>> enumerate(range(5))
<enumerate object at 0x401aa0ec>
>>> list(_)
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
My bad, that was just a misunderstanding on my part. However, enumerate
is still only available in 2.3 so it's not really widely available yet.
The second form he suggested (that of building a second list with range
and zipping the two together) does, however, require building the entire
list, so my objection still applies to that one.
--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ The basis of optimism is sheer terror.
\__/ Oscar Wilde
Bosskey.net / http://www.bosskey.net/
A personal guide to online multiplayer first person shooters.
More information about the Python-list
mailing list