Popular recipes tagged "itertools" and "python" but not "offset"http://code.activestate.com/recipes/tags/itertools-offset+python/2013-11-13T19:30:29-08:00ActiveState Code RecipesAnd yet another round-robin generator (Python)
2013-11-13T19:30:29-08:00Jan Müllerhttp://code.activestate.com/recipes/users/4183984/http://code.activestate.com/recipes/578768-and-yet-another-round-robin-generator/
<p style="color: grey">
Python
recipe 578768
by <a href="/recipes/users/4183984/">Jan Müller</a>
(<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/roundrobin/">roundrobin</a>).
</p>
<p>I know that there is a <a href="http://code.activestate.com/recipes/528936-roundrobin-generator/">nice recipe</a> already that even made it into the <a href="http://docs.python.org/2/library/itertools.html">Python documentation</a>, but this one is more concise and at the same time simpler.</p>
<pre class="prettyprint"><code>>>> list(roundrobin('ABC', 'D', 'EF'))
['A', 'D', 'E', 'B', 'F', 'C']
</code></pre>
Flattening an arbitrarily deep list (or any iterator) (Python)
2012-04-03T17:13:35-07:00Garretthttp://code.activestate.com/recipes/users/4181290/http://code.activestate.com/recipes/578092-flattening-an-arbitrarily-deep-list-or-any-iterato/
<p style="color: grey">
Python
recipe 578092
by <a href="/recipes/users/4181290/">Garrett</a>
(<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tuple/">tuple</a>).
Revision 6.
</p>
<p>What if you had a list like this: [1, -10, [1,2,[3,4]], xrange(200)], and you just wanted to go through each element in order (wanted it to return a simple list of [1,-10,1,2,3,4,1,2,3,4...199])</p>
<p>I've seen a couple of attempts to flatten arbitrarily deep lists. Many of them involve recursion, like this one: <a href="http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html" rel="nofollow">http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html</a></p>
<p>Recursion is generally considered non-pythonic (at least to my knowledge), so I have used one which just involves simple iterators instead. Also, recursion will fail if the list is too deep (so it wouldn't really be arbitrary, would it?).</p>