Search code examples
pythonlist-comprehension

Python - list comprehension over tuple


I have two arrays inside tuple and need to iterate over it.

recs = ([1,2,3], [4,5,6])
[print(f, s) for f, s in recs]

But got error:

ValueError: too many values to unpack (expected 2)

How can I do it?

P.S. print only for debug example


Solution

  • zip is the function you're after:

    _ = [print(*values, end=' ') for values in zip(*recs)]
    

    values here is the (f, s) tuple. This way it will generalize beyond just 2 lists