"back" list

Is there a way to get a generator / iterator that returns the return value of enumerate :

 from itertools import izip, count enumerate(I) # -> (indx, v) izip(I, count()) # -> (v, indx) 

without pulling itertools ?

+4
source share
3 answers
 ((v, indx) for indx, v in enumerate(I)) 

if you really want to avoid itertools . Why do you need?

+11
source

You can do this with a simple expression:

 ((v, i) for i, v in enumerate(some_iterable)) 

Here is a list to easily see the result:

 >>> [(v, i) for i, v in enumerate(["A", "B", "C"])] [('A', 0), ('B', 1), ('C', 2)] 
+13
source

I am not sure if I understood your question correctly. But here is my solution. Code Based: https://docs.python.org/2/library/functions.html#enumerate

 def enumerate_rev(sequence, start=0): n = start for elem in sequence: yield elem,n n += 1 
+1
source

All Articles