Popular recipes tagged "iterator" but not "generator"http://code.activestate.com/recipes/tags/iterator-generator/2015-01-29T17:12:10-08:00ActiveState Code RecipesOdometer iterator (Python)
2014-09-27T22:35:47-07:00Tomas Nordinhttp://code.activestate.com/recipes/users/4189558/http://code.activestate.com/recipes/578944-odometer-iterator/
<p style="color: grey">
Python
recipe 578944
by <a href="/recipes/users/4189558/">Tomas Nordin</a>
(<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/odometer/">odometer</a>, <a href="/recipes/tags/radix/">radix</a>).
Revision 2.
</p>
<p>A suggestion of an odometer implementation. Each "roll" in the odometer is defined by the caller. It is a list holding any objects, only the length of the list is important.</p>
<p>Can be good to have if something need to be tested in all possible combinations.</p>
Forward iterator (Python)
2015-01-29T17:12:10-08:00Andrea Corbellinihttp://code.activestate.com/recipes/users/4186880/http://code.activestate.com/recipes/578560-forward-iterator/
<p style="color: grey">
Python
recipe 578560
by <a href="/recipes/users/4186880/">Andrea Corbellini</a>
(<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
Revision 2.
</p>
<p>A proxy for iterators that lets you <em>read ahead</em> items without consuming the iterator.</p>
An analogue of enumerate for nested lists. (Python)
2012-12-11T16:59:14-08:00John Crichtonhttp://code.activestate.com/recipes/users/4181975/http://code.activestate.com/recipes/578376-an-analogue-of-enumerate-for-nested-lists/
<p style="color: grey">
Python
recipe 578376
by <a href="/recipes/users/4181975/">John Crichton</a>
(<a href="/recipes/tags/enumerate/">enumerate</a>, <a href="/recipes/tags/index/">index</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/list/">list</a>).
Revision 6.
</p>
<p>A generator function which enables traversal and modification of deeply nested lists. Together with the supplied helper functions it could be useful when working with data stored in deeply
nested lists, particularly when the level of nesting precludes a recursive approach.</p>
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>
lazy ordered unique elements from an iterator (Python)
2011-06-23T16:36:38-07:00Andrew Dalkehttp://code.activestate.com/recipes/users/912777/http://code.activestate.com/recipes/577768-lazy-ordered-unique-elements-from-an-iterator/
<p style="color: grey">
Python
recipe 577768
by <a href="/recipes/users/912777/">Andrew Dalke</a>
(<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/unique/">unique</a>).
Revision 2.
</p>
<p>This implements a "unique" filter. Its input is an iterator of hashable items. It returns an iterator containing only the unique items of the input, in input order. That is, list(unique("cabbage")) produces ["c", "a", "b", "g"]. The implementation is lazy. The function supports the "key" parameter, which provides an alternate form of comparison.</p>
<p>(Note: a better version of this is available from the itertools documentation as unique_everseen )</p>
Fast flatten() with depth control and oversight over which subtrees to expand (Python)
2010-11-26T11:10:01-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577470-fast-flatten-with-depth-control-and-oversight-over/
<p style="color: grey">
Python
recipe 577470
by <a href="/recipes/users/4173535/">Kevin L. Sitze</a>
(<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/optimal_solution/">optimal_solution</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/tuple/">tuple</a>).
</p>
<p>Extremely fast, non-recursive, depth limited flatten with powerful control over which subtrees are to be expanded. If this is what you need then look no further.</p>
peek ahead an iterator (Python)
2010-08-17T13:41:41-07:00Quintijn Hoogenboomhttp://code.activestate.com/recipes/users/4171692/http://code.activestate.com/recipes/577361-peek-ahead-an-iterator/
<p style="color: grey">
Python
recipe 577361
by <a href="/recipes/users/4171692/">Quintijn Hoogenboom</a>
(<a href="/recipes/tags/iter/">iter</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/peek/">peek</a>, <a href="/recipes/tags/peeking/">peeking</a>).
</p>
<p>a merge of recipes 16.7 and 19.18 of the python cookbook (2nd edition).</p>
<p>you can peek ahead more steps if needed, or just use the preview variable for the next item of the list.</p>
PHP 'Struct' port (PHP)
2010-03-28T01:22:31-07:00Jeff Griffithshttp://code.activestate.com/recipes/users/835605/http://code.activestate.com/recipes/577160-php-struct-port/
<p style="color: grey">
PHP
recipe 577160
by <a href="/recipes/users/835605/">Jeff Griffiths</a>
(<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/oop/">oop</a>).
Revision 2.
</p>
<p>In Ruby, the <a href="http://ruby-doc.org/core/classes/Struct.html">Struct class</a> is a convenient way to create a hash-like object on the fly and use it for your nefarious purposes. PHP 5+ can be convinced to do this type of things as well, it just doesn't have it out of the box. Here is a simple class that implements iterator and allows you to populate the internal data structure similar to how Ruby's Struct works. Syntactic sugar? Probably.</p>
<p>Note: I haven't bothered to implement the Ruby Struct API per se, Instead I just got something similar by implementing the Iterator interface and keeping things very PHP-like.</p>
iter_except -- a useful variant of __builtin__.iter() (Python)
2010-03-27T02:04:22-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577155-iter_except-a-useful-variant-of-__builtin__iter/
<p style="color: grey">
Python
recipe 577155
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
</p>
<p>Variant of iter(func, sentinel) that looks for an exception rather than for a sentinel value. Good for making iterators from of APIs that advance over a data and return an exception when they are done.</p>
Partition an iterable into n lists (Python)
2009-05-30T16:35:27-07:00Ian Eloffhttp://code.activestate.com/recipes/users/2227021/http://code.activestate.com/recipes/576785-partition-an-iterable-into-n-lists/
<p style="color: grey">
Python
recipe 576785
by <a href="/recipes/users/2227021/">Ian Eloff</a>
(<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/partition/">partition</a>).
</p>
<p>This could also be easily modified to return n iterators, but was outside of my needs. Handy for splitting up the workload for use with multiple threads/processes.</p>