<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-7039308593110047510.post7335400378177623238..comments</id><updated>2011-10-30T06:16:02.787-07:00</updated><category term='travel'/><category term='python'/><title type='text'>Comments on Jarrod Millman: What's the best way to interleave two Python lists...</title><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://blog.jarrodmillman.com/feeds/7335400378177623238/comments/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default'/><link rel='alternate' type='text/html' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html'/><author><name>Jarrod Millman</name><uri>http://www.blogger.com/profile/11547359491745541645</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://3.bp.blogspot.com/-bC2tvEF97qQ/TYUmoH1v9mI/AAAAAAAAAEI/ogkBqpD1Sec/s1600/41d3af311e5a84d652abffa2341165cd.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7039308593110047510.post-2913891378671023795</id><published>2010-10-13T17:44:12.117-07:00</published><updated>2010-10-13T17:44:12.117-07:00</updated><title type='text'>Since you liked zip, why not to like reduce (altho...</title><content type='html'>Since you liked zip, why not to like reduce (although iirc Guido doesn&amp;#39;t like it ;-)), so for the actual interleave just use both functions you would like:&lt;br /&gt;&lt;br /&gt;*In [1]: reduce(tuple.__add__, zip([1,2],[11,21]))&lt;br /&gt;Out[2]: (1, 11, 2, 21)&lt;br /&gt;&lt;br /&gt;?</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/2913891378671023795'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/2913891378671023795'/><link rel='alternate' type='text/html' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html?showComment=1287017052117#c2913891378671023795' title=''/><author><name>yarikoptic</name><uri>http://www.blogger.com/profile/06648933072290144986</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html' ref='tag:blogger.com,1999:blog-7039308593110047510.post-7335400378177623238' source='http://www.blogger.com/feeds/7039308593110047510/posts/default/7335400378177623238' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-287049669'/></entry><entry><id>tag:blogger.com,1999:blog-7039308593110047510.post-7150040612754791175</id><published>2010-10-11T13:25:09.604-07:00</published><updated>2010-10-11T13:25:09.604-07:00</updated><title type='text'>I would use itertools.chain:

from itertools impor...</title><content type='html'>I would use itertools.chain:&lt;br /&gt;&lt;br /&gt;from itertools import chain&lt;br /&gt;&lt;br /&gt;a = list(&amp;#39;abcde&amp;#39;)&lt;br /&gt;b = list(&amp;#39;12345&amp;#39;)&lt;br /&gt;&lt;br /&gt;list(chain(*zip(a,b)))&lt;br /&gt;&lt;br /&gt;[&amp;#39;a&amp;#39;, &amp;#39;1&amp;#39;, &amp;#39;b&amp;#39;, &amp;#39;2&amp;#39;, &amp;#39;c&amp;#39;, &amp;#39;3&amp;#39;, &amp;#39;d&amp;#39;, &amp;#39;4&amp;#39;, &amp;#39;e&amp;#39;, &amp;#39;5&amp;#39;]&lt;br /&gt;&lt;br /&gt;It can be extended to merge more than two lists:&lt;br /&gt;&lt;br /&gt;c = list(&amp;#39;XYZZY&amp;#39;)&lt;br /&gt;list(chain(*zip(a,b,c)))&lt;br /&gt;&lt;br /&gt;If the lists are long, or are iterables, or you want to merge the lists lazily, use iterttools.izip:&lt;br /&gt;&lt;br /&gt;from itertools import chain, count, izip&lt;br /&gt;&lt;br /&gt;for value in chain(*izip(count(),a+b+c)):&lt;br /&gt;   print value</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/7150040612754791175'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/7150040612754791175'/><link rel='alternate' type='text/html' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html?showComment=1286828709604#c7150040612754791175' title=''/><author><name>Mike</name><uri>http://www.blogger.com/profile/05323519519255204306</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html' ref='tag:blogger.com,1999:blog-7039308593110047510.post-7335400378177623238' source='http://www.blogger.com/feeds/7039308593110047510/posts/default/7335400378177623238' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-2046017747'/></entry><entry><id>tag:blogger.com,1999:blog-7039308593110047510.post-7896097210390990982</id><published>2010-10-11T03:52:40.521-07:00</published><updated>2010-10-11T03:52:40.521-07:00</updated><title type='text'>another variation of solution 7:

reduce(lambda ou...</title><content type='html'>another variation of solution 7:&lt;br /&gt;&lt;br /&gt;reduce(lambda out, p: out + [p, p+&amp;#39;64&amp;#39;], paths, list())</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/7896097210390990982'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/7896097210390990982'/><link rel='alternate' type='text/html' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html?showComment=1286794360521#c7896097210390990982' title=''/><author><name>remosu</name><uri>http://www.blogger.com/profile/00044490115353960449</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://lh3.google.com/image/silvio.rene/RaSlWrxcwpI/AAAAAAAAAAY/pGJKsUXscGE/Graphic41.jpg'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html' ref='tag:blogger.com,1999:blog-7039308593110047510.post-7335400378177623238' source='http://www.blogger.com/feeds/7039308593110047510/posts/default/7335400378177623238' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-1033594902'/></entry><entry><id>tag:blogger.com,1999:blog-7039308593110047510.post-6220903083917413096</id><published>2010-10-11T01:41:32.844-07:00</published><updated>2010-10-11T01:41:32.844-07:00</updated><title type='text'>Or using a bit from the previous comment (and rela...</title><content type='html'>Or using a bit from the previous comment (and relating it to your example):&lt;br /&gt;&lt;br /&gt;out = []&lt;br /&gt;map(out.extend,([p, p + &amp;#39;64&amp;#39;] for p in paths))</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/6220903083917413096'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/6220903083917413096'/><link rel='alternate' type='text/html' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html?showComment=1286786492844#c6220903083917413096' title=''/><author><name>JoeK</name><uri>http://www.blogger.com/profile/11130155105330913107</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html' ref='tag:blogger.com,1999:blog-7039308593110047510.post-7335400378177623238' source='http://www.blogger.com/feeds/7039308593110047510/posts/default/7335400378177623238' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-593525012'/></entry><entry><id>tag:blogger.com,1999:blog-7039308593110047510.post-5373698681888032873</id><published>2010-10-11T01:36:17.672-07:00</published><updated>2010-10-11T01:36:17.672-07:00</updated><title type='text'>Here&amp;#39;s another simple one (note that you are t...</title><content type='html'>Here&amp;#39;s another simple one (note that you are throwing away the result of the map statement).&lt;br /&gt;&lt;br /&gt;1&amp;gt; A = [1,2,3,4,5]&lt;br /&gt;2&amp;gt; B = [6,7,8,9,10]&lt;br /&gt;3&amp;gt; out = []&lt;br /&gt;4&amp;gt; map(out.extend,zip(A,B))&lt;br /&gt;&amp;lt;4&amp;gt; [None, None, None, None, None]&lt;br /&gt;5&amp;gt; out&lt;br /&gt;&amp;lt;5&amp;gt; [1, 6, 2, 7, 3, 8, 4, 9, 5, 10]</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/5373698681888032873'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/5373698681888032873'/><link rel='alternate' type='text/html' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html?showComment=1286786177672#c5373698681888032873' title=''/><author><name>JoeK</name><uri>http://www.blogger.com/profile/11130155105330913107</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html' ref='tag:blogger.com,1999:blog-7039308593110047510.post-7335400378177623238' source='http://www.blogger.com/feeds/7039308593110047510/posts/default/7335400378177623238' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-593525012'/></entry><entry><id>tag:blogger.com,1999:blog-7039308593110047510.post-1457077369722171554</id><published>2010-10-10T15:14:58.410-07:00</published><updated>2010-10-10T15:14:58.410-07:00</updated><title type='text'>Good thing there&amp;#39;s only one way to do it with ...</title><content type='html'>Good thing there&amp;#39;s only one way to do it with Python...  :-)</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/1457077369722171554'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/1457077369722171554'/><link rel='alternate' type='text/html' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html?showComment=1286748898410#c1457077369722171554' title=''/><author><name>Lucas</name><uri>http://www.blogger.com/profile/13001386543414706674</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html' ref='tag:blogger.com,1999:blog-7039308593110047510.post-7335400378177623238' source='http://www.blogger.com/feeds/7039308593110047510/posts/default/7335400378177623238' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-863426126'/></entry><entry><id>tag:blogger.com,1999:blog-7039308593110047510.post-1540710527083943609</id><published>2010-10-10T14:35:55.494-07:00</published><updated>2010-10-10T14:35:55.494-07:00</updated><title type='text'>A variation of solution 6:
&lt;i&gt;[item for items in (...</title><content type='html'>A variation of solution 6:&lt;br /&gt;&lt;i&gt;[item for items in ([p, p + &amp;#39;64&amp;#39;] for p in paths) for item in items]&lt;/i&gt;. No need for the &lt;i&gt;zip&lt;/i&gt; just use a generator expression.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/1540710527083943609'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/1540710527083943609'/><link rel='alternate' type='text/html' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html?showComment=1286746555494#c1540710527083943609' title=''/><author><name>Mike Müller</name><uri>http://www.blogger.com/profile/15418113987963668896</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html' ref='tag:blogger.com,1999:blog-7039308593110047510.post-7335400378177623238' source='http://www.blogger.com/feeds/7039308593110047510/posts/default/7335400378177623238' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-701751493'/></entry><entry><id>tag:blogger.com,1999:blog-7039308593110047510.post-7509489009446436412</id><published>2010-10-10T06:51:49.180-07:00</published><updated>2010-10-10T06:51:49.180-07:00</updated><title type='text'>I also like solutions 5 as a general solution, tho...</title><content type='html'>I also like solutions 5 as a general solution, though stylistically, I prefer &amp;quot;map(out.extend, ([p, p + &amp;#39;64&amp;#39;] for p in paths))&amp;quot; to the for loop.  I think yours is the more pythonic solution (and might even be more efficient).  I like to use map if all a for loop does is call a function on each element of an iterable, since I think it&amp;#39;s a bit clearer and it saves a line of code.&lt;br /&gt;&lt;br /&gt;Another solution (a modification of solution 7) is &amp;quot;return sum(([p, p + &amp;#39;64&amp;#39;] for p in paths), [])&amp;quot;, which saves an import.  I&amp;#39;d probably go with that since it&amp;#39;s shorter, and efficiency only matters if the lists are extremely long or this function gets called a lot, neither of which is true in your application.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/7509489009446436412'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7039308593110047510/7335400378177623238/comments/default/7509489009446436412'/><link rel='alternate' type='text/html' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html?showComment=1286718709180#c7509489009446436412' title=''/><author><name>Lucas</name><uri>http://www.blogger.com/profile/13001386543414706674</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://blog.jarrodmillman.com/2010/10/whats-best-way-to-interleave-two-python.html' ref='tag:blogger.com,1999:blog-7039308593110047510.post-7335400378177623238' source='http://www.blogger.com/feeds/7039308593110047510/posts/default/7335400378177623238' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-863426126'/></entry></feed>
