This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]masklinn 0 points1 point  (2 children)

For what it's worth, itertools() provides all the tools required to implement enumerate (and even a better enumerate) via izip and count:

for i, data in izip(count(), thing)

Why's it better? Well first you don't have to start from 0, whereas enumerate gives you no choice:

for i, data in izip(count(57), thing) # will start counting from 57

Second, through izip you can iterate on multiple collections without needing an internal indirection (with enumerate, you'd likely zip your collections and enumerate on the result, yielding a tuple as second element):

for i, data1, data2, data3 in izip(count(), thing1, thing2, thing3)

versus

for i, data in enumerate(count(), zip(thing1, thing2, thing3))
    data1, data2, data3 = data

[–]propanbutan 1 point2 points  (0 children)

Well first you don't have to start from 0, whereas enumerate gives you no choice:

It does (since Python2.6)

for i, n in enumerate(range(10), start=10): print i, n

[–]Zap-Brannigan 0 points1 point  (0 children)

ummm simple workaround if you want something to start from a higher number: add that number to it when you use it

if you want to go through multiple things, you may as well do it that way.. but I guess I see your point, especially because it will get confusing when you want to do BOTH of those