Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine enumerate + itertools.izip in Python

I would like to iterate + enumerate over two lists in Python. The following code looks ugly. Is there any better solution?

for id, elements in enumerate(itertools.izip(as, bs)):
  a = elements[0]
  b = elements[1]
  # do something with id, a and b

Thank you.

like image 510
Nikolay Vyahhi Avatar asked Nov 27 '22 14:11

Nikolay Vyahhi


1 Answers

You can assign a and b during the for loop:

for id, (a, b) in enumerate(itertools.izip(as, bs)):
  # do something with id, a and b
like image 140
Peter Collingridge Avatar answered Dec 16 '22 10:12

Peter Collingridge