I have a python-3 code that I would like to be compatible for both python-2 and python-3 keeping the code as is as much as possible. I would like to use the iteration behavior of range (to iterate over many items).
In order to get an iterator:
- python-2 uses xrange(N)
- python-3 uses range(N)
what is the best way to make it an iterator for python2 with minimal changes as possible?
looking at this link, it suggests a few ways for range and xrange but couldn't make it work
range = xrangeif you find the user running python 2. Basically, deliberately redefinerangexrangein python 2 norrangein python 3 return iterators, they return anxrangeorrangeobject and you can't use them withnext().