1 # Copyright (C) 2010-2012 W. Trevor King <wking@tremily.us>
3 # This file is part of Hooke.
5 # Hooke is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with Hooke. If not, see <http://www.gnu.org/licenses/>.
18 from __future__ import absolute_import
20 from itertools import izip
23 def reverse_enumerate(x):
24 """Iterate through `enumerate(x)` backwards.
26 This is a memory-efficient version of `reversed(list(enumerate(x)))`.
31 >>> a = ['a', 'b', 'c']
32 >>> it = reverse_enumerate(a)
34 <type 'itertools.izip'>
36 [(2, 'c'), (1, 'b'), (0, 'a')]
37 >>> list(reversed(list(enumerate(a))))
38 [(2, 'c'), (1, 'b'), (0, 'a')]
42 `Original implemenation`_ by Christophe Simonis.
44 .. _Original implementation:
45 http://christophe-simonis-at-tiny.blogspot.com/2008/08/python-reverse-enumerate.html
47 return izip(xrange(len(x)-1, -1, -1), reversed(x))
49 # LocalWords: itertools