1

I'm trying iterate over two lists in python. When I do it the way below it only prints over the first three items in list lstTwo.

My code looks something like this:

lstOne = ["pmCat","pmDog","pmMouse"]
lstTwo = ['0', '197', '0', '0', '0', '0', '1', '0', '4', '0', '197', '0']

for (y,z) in zip(lstOne,lstTwo):
    print(y,z)

I tried zip_longest, but it gives you the entire second list but only the first three out of the first list.

I'm trying to have it print something like:
pmCat 0
pmDog 197
pmMouse 0
pmCat 0
pmDog 0 pmMouse 0
pmCat 1
pmDog 0
pmMouse 4
pmCat 0
pmDog 197
pmMouse 0

  • 1
    You probably should use [`zip_longest()`](https://docs.python.org/3/library/itertools.html#itertools.zip_longest). – Delgan Sep 14 '16 at 08:49
  • Inspect the output of `zip(listOne, lstTwo)` and you'll see you're actually trying to iterate over a datastructure that looks like this `[('pmCat', '0'), ('pmDog', '197'), ('pmMouse', '0')]` which should explain why it only prints the first three elements – Jack Evans Sep 14 '16 at 08:49
  • I tried zip_longest, but it gives you the entire second list but only the first three out of the first list. I'm trying to have it print something like:
    pmCat 0
    pmDog 197
    pmMouse 0
    pmCat 0
    pmDog 0 pmMouse 0
    pmCat 1
    pmDog 0
    pmMouse 4
    pmCat 0
    pmDog 197
    pmMouse 0
    – GenericUser Sep 14 '16 at 09:14

0 Answers0