Mar-28-2022, 04:59 PM
(This post was last modified: Mar-28-2022, 05:00 PM by deanhystad.)
Quote:It is trying to unpack 3 values (ax, bx, cx) into xp, yp, ?for xp, yp in zip(a,b,c): print(xp) This outputs ValueError: too many values to unpack (expected 2)It expected two (for two variables) and it got four, right? Why are two variables "too many values to unpack" when one variable is acceptable?
Quote:So I'm seeing how zip() works in some different contexts but I'm still a bit fuzzy as to why.Zip is always working the same. The difference is what is being unpacked; an entire sequence or one iteration of a sequence. zip(a, b, c) always generates a sequence of tuples (ax, bx, cx). When you unpack in a for loop, you are unpacking the tuple, one iteration of the sequence.
xp, yp, m = (ax, bx, cx)When you unpack outside the for loop you are unpacking the entire sequence.
xp, yp, m, n = ((a0, b0, c0), (a1, b1, c1), (a2, b2, c2), (a3, b3, c3))This is no different at all than:
for a in (1, 2, 3, 4): print(a)versus
a, b, c, d = (1, 2, 3, 4)The difference has to do with what you are unpacking, not zip. And what you are unpacking has to do with where you are unpacking.