简体   繁体   中英

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?

PS print only for debug example

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

Do you just want to iterate over each of the 2 arrays? Like: [print(array) for array in recs]

Or do you want to iterate over the values in each array as well, like: [[print(num) for num in array] for array in recs]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM