## Python enumerate reverse index only

python list iteration reverse
how to reverse loop a list in python
python count backwards through list
reverse a list in python without reverse function
python enumerate count backwards
python iter backwards
list comprehension reverse
write an iterator class that takes a list and iterates it from the reverse direction in python

I am trying to reverse the index given by `enumerate` whilst retaining the original order of the list being enumerated.

Assume I have the following:

```>> range(5)
[0, 1, 2, 3, 4]
```

If I enumerate this I would get the following:

```>> list(enumerate(range(5)))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
```

However I want to reverse the index provided by enumerate so that I get:

```[(4, 0), (3, 1), (2, 2), (1, 3), (0, 4)]
```

So far I have the following code:

```reversed(list(enumerate(reversed(range(5)))))
```

I was just wondering if there was a neater way to do this?

```>>> zip(range(9, -1, -1), range(10))
[(9, 0), (8, 1), (7, 2), (6, 3), (5, 4), (4, 5), (3, 6), (2, 7), (1, 8), (0, 9)]

>>> def reversedEnumerate(l):
return zip(range(len(l)-1, -1, -1), l)
>>> reversedEnumerate(range(10))
[(9, 0), (8, 1), (7, 2), (6, 3), (5, 4), (4, 5), (3, 6), (2, 7), (1, 8), (0, 9)]
```

As @julienSpronk suggests, use `izip` to get a generator, also `xrange`:

```import itertools
>>> import itertools
>>> def reversedEnumerate(l):
...     return itertools.izip(xrange(len(l)-1, -1, -1), l)
...
>>> reversedEnumerate(range(10))
<itertools.izip object at 0x03749760>
>>> for i in reversedEnumerate(range(10)):
...     print i
...
(9, 0)
(8, 1)
(7, 2)
(6, 3)
(5, 4)
(4, 5)
(3, 6)
(2, 7)
(1, 8)
(0, 9)
```

Traverse a list in reverse order in Python, If you need the loop index, and don't want to traverse the entire list twice, 1 less than len(collection) , keep going until you get to just before -1, by steps of -1. Varun February 11, 2018 Python : Different ways to Iterate over a List in Reverse Order 2018-02-11T16:29:13+05:30 List, Python No Comment In this article we will discuss different ways to Iterate over a python list in reverse order.

I don't know if this solution is better for you, but at least it's shorter:

```>>> [(4 - x, x) for x in range(5)]
[(4, 0), (3, 1), (2, 2), (1, 3), (0, 4)]
```

Python: reverse enumerate, For getting the index, it's easy: we use the builtin function enumerate. Now, to reverse the order we just reverse all the izip arguments str = "Python" for idx, ch in enumerate(str): print("index is %d and character is %s" \ % (idx, ch)) And the output will be: index is 0 and character is P index is 1 and character is y index is 2 and character is t index is 3 and character is h index is 4 and character is o index is 5 and character is n.

Just take the length of your list and subtract the index from that...

```L = range(5)

for i, n in L:
my_i = len(L) -1 - i
...
```

Or if you really need a generator:

```def reverse_enumerate(L):
# Only works on things that have a len()
l = len(L)
for i, n in enumerate(L):
yield l-i-1, n
```

`enumerate()` can't possibly do this, as it works with generic iterators. For instance, you can pass it infinite iterators, that don't even have a "reverse index".

Re: reversed(enumerate(x)) « python-list « ActiveState List Archives, Than something that let you "enumerate the reverse" of something. obj > reversed() only operates on sequences, so it can't operate on arbitrary> pair values with their indices, coalesce the result as a list, and then iterate  Python List reverse() Python List reverse() The reverse() method reverses the elements of the list. Python Reference. Python List index() Python Reference.

Assuming your list is not long and you will not run into performance errors, you may use `list(enumerate(range(5)[::-1]))[::-1]`.

Test:

```>>> list(enumerate(range(5)[::-1]))[::-1] [(0, 4), (1, 3), (2, 2), (3, 1), (4, 0)]```

How to Reverse a List in Python – dbader.org, How do you iterate a list in reverse order python? Python List reverse() Python List sort() The index() method only returns the first occurrence of the matching element. Python Reference. Python List index()

Actually I'm using the same logic as @RemcoGerlich did, but I use `list comprehension` directly, which make the code now become 1-liner:

```def generatelist(x):
return [(x-1-i,n) for i,n in enumerate(range(x))]
```

Regarding the dilemma of choosing `generator` or `list comprehension`, here is the suggested way:

Basically, use a generator expression if all you're doing is iterating once. If you want to store and use the generated results, then you're probably better off with a list comprehension.

Python : Different ways to Iterate over a List in Reverse Order , How do you reverse a list without reverse in Python? The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x). list. remove ( x ) Remove the first item from the list whose value is equal to x .

Python enumerate reverse index only – Icetutor, A step-by-step tutorial on the three main ways to reverse a Python list or Note that this only Duration: 17:42 Posted: Jul 10, 2017 Python list method reverse() reverses objects of list in place. Syntax. Following is the syntax for reverse() method − list.reverse() Parameters. NA. Return Value. This method does not return any value but reverse the given object from the list. Example. The following example shows the usage of reverse() method.

Traverse a list in reverse order in Python, Python : Different ways to Iterate over a List in Reverse Order We don't want to change the order in existing list, just want to iterate in reverse. iterating row by row · Python : How to access characters in string by index ? Adding the “[1:3]” index tells Python to give us a slice of the list from index 1 to index 2. To avoid off-by-one errors it’s important to remember that the upper bound is exclusive —this is why we only got [2, 3] as the sub-list from the [1:3] slice.

Python List reverse(), How about using zip instead with a reversed range? >>> zip(range(9, -1, -1), range(10)) [(9, 0), (8, 1), (7, 2), (6, 3), (5, 4), (4, 5), (3, 6), (2, 7), (1,  The index() method finds the first occurrence of the specified value. The index() method raises an exception if the value is not found. The index() method is almost the same as the find() method, the only difference is that the find() method returns -1 if the value is not found. (See example below)