1

I'm trying to zip two lists of different lengths in Python so the items in the the second one are zipped iteratively over the elements in the first one.

I have tried using itertools.product without much luck. Is it possible to achieve this with some module in the standard library?

E.g

import itertools
a = ["a", "b", "c", "d", "e", "f"]
b = [1, 2, 3]
list(itertools.product(a, b)) 

Result:

 [('a', 1),
 ('a', 2),
 ('a', 3),
 ('b', 1),
 ('b', 2),
 ('b', 3),
 ('c', 1),
 ('c', 2),
 ('c', 3)
 [...],

Desired result:

 [('a', 1),
 ('b', 2),
 ('c', 3),
 ('d', 1),
 ('e', 2),
 ('f', 3)]
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
scetoaux
  • 398
  • 7
  • 19

1 Answers1

4

Use itertools.cycle to infinitely repeat you smaller list

zip(a, itertools.cycle(b))
languitar
  • 6,554
  • 2
  • 37
  • 62