Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unpacking zip object
#11
Thanks Dean!
Reply
#12
(Mar-26-2022, 06:30 PM)deanhystad Wrote: What you were missing is the intermediate step int he packing/unpacking.
zip(a, b, c) produces this series:
('1-6-2017', 265, 'd'), ('1-13-2017', -10, ''), ('1-20-2017', 130, 'd'), ('1-27-2017', 330, '')
or
(a0, b0, c0), (a1, b1, c1), (a2, b2, c2), (a3, b3, c3)

When you do xp, yp, m = zip(a, b, c) it tries to unpack that series of 4 into 3 variables. 4 does not fit into 3, so you get an error.

When you do "for xp, yp, m in zip(a, b, c): you are unpacking the tuple (ax, bx, cx) into xp, yp and m.

To better understand, I've tried these cases:

for xp in zip(a,b,c):
    print(xp)
This outputs a tuple.

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?

for xp, yp, m in zip(a,b,c):
    print(xp)
No more tuples: value from a gets assigned to xp, value from b gets assigned to yp, and value from c gets assigned to m.

for xp, yp, m, n in zip(a,b,c):
    print(xp)
ValueError: not enough values to unpack (expected 4, got 3). I see why four were expected (xp, yp, m, n), but... why wouldn't each tuple (a,b,c) be assigned to a variable?

Actually, without the for loop, each tuple gets assigned to a variable:

xp, yp, m, n = zip(a,b,c)
print(xp)
Unlike the for loop where this was "not enough values to unpack," this outputs the first tuple.

So I'm seeing how zip() works in some different contexts but I'm still a bit fuzzy as to why. Sorry to belabor the point.
Reply
#13
Quote:
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?
It is trying to unpack 3 values (ax, bx, cx) into xp, yp, ?
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unpacking a dict with * or ** msrk 4 999 Dec-02-2023, 11:50 PM
Last Post: msrk
  iterate through the dict_values while unpacking the dictionary PeacockOpenminded 3 1,315 Jan-22-2023, 12:44 PM
Last Post: PeacockOpenminded
  unpacking list wardancer84 2 1,895 Sep-11-2021, 02:42 PM
Last Post: wardancer84
  unpacking tuple not working project_science 1 1,497 Jan-09-2021, 09:09 PM
Last Post: buran
  Unpacking a list Mark17 3 2,629 Dec-18-2020, 05:08 AM
Last Post: aajkaal
  Unpacking wheel files DavidTheGrockle 3 11,446 Dec-15-2020, 05:11 PM
Last Post: DavidTheGrockle
  Why the result of "extended iterable unpacking" with set() is unpredictable? zohanlin 2 2,083 Jun-29-2020, 10:30 AM
Last Post: zohanlin
  Unpacking nested lists yonatan776 1 2,214 Apr-14-2020, 08:50 PM
Last Post: buran
  Unpacking dictionary from .xlsx as Kwargs etjkai 5 2,865 Dec-27-2019, 05:31 PM
Last Post: etjkai
  Tuple Unpacking HarshaliPatel 3 2,850 Jan-30-2019, 12:42 PM
Last Post: dukoolsharma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020