superx
superx

Reputation: 167

Python - iterate two lists and with increment variable

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)

Upvotes: 2

Views: 835

Answers (2)

Jean-François Fabre
Jean-François Fabre

Reputation: 140256

Just use enumerate and nested unpacking:

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

Upvotes: 3

Netwave
Netwave

Reputation: 42746

Use itertools.count:

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

Upvotes: 2

Related Questions