Top-rated recipes by Michael Chermside http://code.activestate.com/recipes/users/98086/top/2002-08-02T08:31:54-07:00ActiveState Code RecipesAssociating multiple values with each key in a dictionary. (Python) 2001-03-09T18:37:31-08:00Michael Chermsidehttp://code.activestate.com/recipes/users/98086/http://code.activestate.com/recipes/52219-associating-multiple-values-with-each-key-in-a-dic/ <p style="color: grey"> Python recipe 52219 by <a href="/recipes/users/98086/">Michael Chermside</a> . </p> <p>A normal dictionary performs a mapping of the form: key -> value. Suppose you want a dictionary which maps each key to multiple values: key -> [value*]. Here's an easy and efficient way to achieve it. We rely on the setdefault() method of dictionarys both initialize the list of values for this key if necessary, and give us the list at the same time.</p> List iterator with advance() and regress(). (Python) 2002-08-02T08:31:54-07:00Michael Chermsidehttp://code.activestate.com/recipes/users/98086/http://code.activestate.com/recipes/142435-list-iterator-with-advance-and-regress/ <p style="color: grey"> Python recipe 142435 by <a href="/recipes/users/98086/">Michael Chermside</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>The basic iterator for a list is a very "fast-but-dumb" design. It doesn't allow one to skip forward (except with "continue"), or backward (at all), nor does it behave well if the list is modified while it is being traversed. The following is NOT the be-all and end-all of improved iterators, but it gives a few ideas of how a better one might be created.</p> Releasing a Resource used by a Generator (Python) 2002-05-31T05:00:15-07:00Michael Chermsidehttp://code.activestate.com/recipes/users/98086/http://code.activestate.com/recipes/130004-releasing-a-resource-used-by-a-generator/ <p style="color: grey"> Python recipe 130004 by <a href="/recipes/users/98086/">Michael Chermside</a> . </p> <p>Since try-finally can't be used in a generator, it can be somewhat tricky to ensure that a resource is released if the generator is exited early. This solution wraps the generator with an enclosing class to ensure the resource is released.</p>