2

I wanted to iterate through two lists at the same time while also being able to increment a variable: I am able to achieve this but incrementing row manually.

for name, time in zip(Linklist, Timelist):

    worksheet.write(row, col, name)
    worksheet.write(row, col + 1, time)
    row=row+1

Trying to see if something like this can be done:

for row, name, time in zip(Linklist, Timelist):

    worksheet.write(row, col, name)
    worksheet.write(row, col + 1, time)

2 Answers 2

3

Just use enumerate and nested unpacking:

for row, (name, time) in enumerate(zip(Linklist, Timelist)):
Sign up to request clarification or add additional context in comments.

Comments

2

Use itertools.count:

for row, name, time in zip(itertools.count(), Linklist, Timelist):
    ...

3 Comments

enumerate(x) is the usual way to do zip(count(), x).
@zvone, well for only one collection maybe yes, for more maybe is your way. I usually tend to use a single unpack (not nested). Either way there is another answer with that.
I'm pretty sure yours work too but the first answer works better at this point. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.