Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unpacking zip object
#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


Messages In This Thread
Unpacking zip object - by Mark17 - Mar-24-2022, 06:52 PM
RE: Unpacking zip object - by bowlofred - Mar-24-2022, 07:41 PM
RE: Unpacking zip object - by Mark17 - Mar-24-2022, 09:32 PM
RE: Unpacking zip object - by Coricoco_fr - Mar-24-2022, 07:51 PM
RE: Unpacking zip object - by bowlofred - Mar-24-2022, 10:09 PM
RE: Unpacking zip object - by Mark17 - Mar-25-2022, 02:21 PM
RE: Unpacking zip object - by deanhystad - Mar-25-2022, 07:27 PM
RE: Unpacking zip object - by Mark17 - Mar-26-2022, 04:12 PM
RE: Unpacking zip object - by deanhystad - Mar-26-2022, 04:20 PM
RE: Unpacking zip object - by deanhystad - Mar-26-2022, 06:30 PM
RE: Unpacking zip object - by Mark17 - Mar-28-2022, 02:50 PM
RE: Unpacking zip object - by Mark17 - Mar-28-2022, 12:39 PM
RE: Unpacking zip object - by deanhystad - Mar-28-2022, 04:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Unpacking a dict with * or ** msrk 4 1,023 Dec-02-2023, 11:50 PM
Last Post: msrk
  iterate through the dict_values while unpacking the dictionary PeacockOpenminded 3 1,337 Jan-22-2023, 12:44 PM
Last Post: PeacockOpenminded
  unpacking list wardancer84 2 1,904 Sep-11-2021, 02:42 PM
Last Post: wardancer84
  unpacking tuple not working project_science 1 1,516 Jan-09-2021, 09:09 PM
Last Post: buran
  Unpacking a list Mark17 3 2,657 Dec-18-2020, 05:08 AM
Last Post: aajkaal
  Unpacking wheel files DavidTheGrockle 3 11,665 Dec-15-2020, 05:11 PM
Last Post: DavidTheGrockle
  Why the result of "extended iterable unpacking" with set() is unpredictable? zohanlin 2 2,107 Jun-29-2020, 10:30 AM
Last Post: zohanlin
  Unpacking nested lists yonatan776 1 2,230 Apr-14-2020, 08:50 PM
Last Post: buran
  Unpacking dictionary from .xlsx as Kwargs etjkai 5 2,910 Dec-27-2019, 05:31 PM
Last Post: etjkai
  Tuple Unpacking HarshaliPatel 3 2,890 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