Search code examples
pythonzipenumerate

Looping over multiple lists with enumerate


It looks like enumerate and zip don't work together in Python 3?

alist = ['a1', 'a2', 'a3']
blist = ['b1', 'b2', 'b3']

for i, a, b in enumerate(zip(alist, blist)):
    print(i, a, b)

Returns 'int' object is not callable


Solution

  • Add () around a,b. The unpacking of the values is for the enumerate function which returns tuples of size two: index and value. If you further want to unpack the value item then as below:

    for i, (a, b) in enumerate(zip(alist, blist)):
        print(i, a, b)