Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unpacking zip object
#8
(Mar-25-2022, 07:27 PM)deanhystad Wrote: I can't tell if this is a confusion about iterators or a confusion about unpacking question.

As bowlofred said, zip returns an iterator. You give it an iterable. It returns an iterator. Each time you call next(iterator) you get the next item in the iterable.
...
Python supports packing and unpacking sequences.  An iterator is a sequence, so you can unpack an iterator.
[python]a = ['1-6-2017', '1-13-2017', '1-20-2017', '1-27-2017']
b = [265, -10, 130, 330]
c = ['d', '', 'd', '']
x, y, *z = zip(a, b, c)
print(x, y, z)
Output:
('1-6-2017', 265, 'd') ('1-13-2017', -10, '') [('1-20-2017', 130, 'd'), ('1-27-2017', 330, '')]
I told Python that z will take all remaining values from the iterator, and when printing you can see that z contains two values, not one. This makes sense. a, b and c each are each len 4. zip(a, b, c) will produce 4 values before raising the StopIteration exception. Unpacking puts the first item in x, the second in y, and the two remaining items in z. There are 4 items to unpack. 4 items cannot be unpacked into 3 variables (well, they can, but you know what I mean).

...

What I took from bowlofred was the "too many items to unpack" Traceback referred to a total of 12 items trying to be unpacked into 3 variables. What you're saying here, though, is that it's about four sets being unpacked into three variables. I have misunderstood on this point. I have expected values from a to go in xp, values from b to go in yp, and values from c to go in m. I now believe it's the first grouping of x, y, z going into xp, the second grouping of x, y, z going into yp, and the remaining (with the * operator) groupings going into m. I'll have to go back to bowlofred's comments and see how they compare to the example you gave.

The question here that pertains most to my current application is why does the first ax.plot() output a line while the second ax.plot outputs a set of markers (or lack thereof if I indicate None):

a = ['1-6-2017', '1-13-2017', '1-20-2017', '1-27-2017']
b = [265, -10, 130, 330]
c = ['d', '', 'd', '']

fig, ax = plt.subplots(1)

ax.plot(a,b) #This plots line.
plt.xticks(rotation=90) 
for xp, yp, m in zip(a, b, c): #need for-loop (else "too many values to unpack")
    ax.plot(xp,yp,m,color='orange',markersize=12) #this plots markers
plt.tight_layout() #this doesn't seem to do anything?
plt.show()
I'm going to take a guess. ax.plot() is a method that plots line graphs. However, if you present just one value at a time, then it's only going to plot the point. The second line is a loop that revisits ax.plot() several times but each time, presents only one set of values to it.
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,084 Dec-02-2023, 11:50 PM
Last Post: msrk
  iterate through the dict_values while unpacking the dictionary PeacockOpenminded 3 1,378 Jan-22-2023, 12:44 PM
Last Post: PeacockOpenminded
  unpacking list wardancer84 2 1,931 Sep-11-2021, 02:42 PM
Last Post: wardancer84
  unpacking tuple not working project_science 1 1,543 Jan-09-2021, 09:09 PM
Last Post: buran
  Unpacking a list Mark17 3 2,691 Dec-18-2020, 05:08 AM
Last Post: aajkaal
  Unpacking wheel files DavidTheGrockle 3 11,961 Dec-15-2020, 05:11 PM
Last Post: DavidTheGrockle
  Why the result of "extended iterable unpacking" with set() is unpredictable? zohanlin 2 2,142 Jun-29-2020, 10:30 AM
Last Post: zohanlin
  Unpacking nested lists yonatan776 1 2,251 Apr-14-2020, 08:50 PM
Last Post: buran
  Unpacking dictionary from .xlsx as Kwargs etjkai 5 2,968 Dec-27-2019, 05:31 PM
Last Post: etjkai
  Tuple Unpacking HarshaliPatel 3 2,924 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