<div>iBrett,</div>
<div> </div>
<div>iter<br><br></div>
<div class="gmail_quote">On Fri, Sep 16, 2011 at 3:35 AM, Brett Ritter <span dir="ltr"><<a href="mailto:swiftone@swiftone.org">swiftone@swiftone.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">I ran into this article (<br><a href="http://blog.adku.com/2011/09/hodgepodge-of-python.html" target="_blank">http://blog.adku.com/2011/09/hodgepodge-of-python.html</a> ) and found<br>
myself temporarily stymied by one line it in:<br><br>zip(*[iter(a)]*2)<br><br>Used like this:<br><br>>>> a = ['a','1','b','2','c','3']<br>>>> zip(*[iter(a)]*2)<br>
[('a', '1'), ('b', '2'), ('c', '3')]<br><br>While I'm unlikely to use such a construct (if I can't easily follow<br>it now, I or my successor likely won't follow it should it need to be<br>
debugged sometime in the future), I found the education I got in<br>deciphering it was worth the effort. I'm sharing it here so others<br>can benefit from my puzzlement.<br><br>iter(a) returns a list iterator for a. See help(iter) for more.<br>
[iter(a)] is a list containing one element, an iterator. This is<br>created only so we can do the below:<br>[iter(a)]*2 is a list containing two elements, each the SAME list iterator.<br>For simplicity, let's break this out for further analysis:<br>
<br>>>> b = iter(a)<br>>>> c = [b,b]<br></blockquote>
<div> </div>
<div>iter(c) returns listiterator </div>
<div> </div>
<div> </div>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid"><br>*[iter(a)]*2 flattens the list when passed into a function call.<br>Using our more verbose but simple syntax: *c. This only works when<br>
passed to a function.<br>zip() creates tuples each holding the Nth elements from a number of<br>sequences. See help(zip) for more.<br>Thus, zip(a) or zip(a,a) would return:<br>>>> zip(a)<br>[('a',), ('1',), ('b',), ('2',), ('c',), ('3',)]<br>
>>> zip(a,a)<br>[('a', 'a'), ('1', '1'), ('b', 'b'), ('2', '2'), ('c', 'c'), ('3', '3')]<br><br>What happens when we pass an iterator to zip? That's not mentioned in<br>
the docstring blurb.<br>>>> zip(iter(a))<br>[('a',), ('1',), ('b',), ('2',), ('c',), ('3',)]<br>Answer: It works as intended.<br><br>Now we come to the magic of this little snippet.<br>
zip(iter(a),iter(a)) wouldn't work, because each call to iter(a)<br>returns a DIFFERENT iterator.<br>>>> zip(iter(a), iter(a))<br>[('a', 'a'), ('1', '1'), ('b', 'b'), ('2', '2'), ('c', 'c'), ('3', '3')]<br>
<br>But by creating the list of two elements each of which is the SAME<br>iterator, as each is asked to iterate it advances the common element<br>indicator:<br>>>> zip(*c)<br>[('a', '1'), ('b', '2'), ('c', '3')]<br>
Notice that the flattening is required, because zip needs to get<br>multiple arguments:<br>>>> b = iter(a) #our original iterator is spent, so we're assigning a new one<br>>>> c = [b,b]<br>>>> zip(c) #Not flattened, is just a single list, like a.<br>
[(<listiterator object at 0x024E32D0>,), (<listiterator object at 0x024E32D0>,)]<br>>>> zip(b,b) # here it is two iterators sent to zip() (though they happen to be the SAME iterator)<br>[('a', '1'), ('b', '2'), ('c', '3')]<br>
<br>I hope some of you enjoy playing with this, and hopefully someone<br>learned something useful! While I'm not likely to use the listed<br>form, I can very well see myself saying:<br><br>>>> a = ['a','1','b','2','c','3'] #well, I can see myself using this with meaningful variable names<br>
>>> b = iter(a)<br>>>> zip(b,b) # Group in sets of 2 elements<br><font color="#888888"><br>--<br>Brett Ritter / SwiftOne<br><a href="mailto:swiftone@swiftone.org">swiftone@swiftone.org</a><br>_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>To unsubscribe or change subscription options:<br><a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br><i>Satajanus Nig. Ltd<br><br><br></i><br>