Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

itertools 'previous' (opposite of next) python

I'm currently using something like

>> import itertools
>> ABC = [a, b, c]
>> abc = itertools.cycle( ABC )
>> next( abc )
a
>> next( abc )
b    
>> next( abc )
c

I want my next call to be

>> previous( abc )
b

Is there a method in itertools that can accomplish this?

like image 761
Chris Avatar asked Jun 18 '17 05:06

Chris


1 Answers

No, there isn't.

Because of the way Python's iteration protocol works, it would be impossible to implement previous without keeping the entire history of the generated values. Python doesn't do this, and given the memory requirements you probably wouldn't want it to.

like image 169
rici Avatar answered Oct 04 '22 03:10

rici