Skip to content
Advertisement

Easiest way to include a stop parameter in enumerate python?

Ss there a simple way to iterate over an iterable object that allows the specification of an end point, say -1, as well as the start point in enumerate. e.g.

JavaScript

So if my object has a length of 10, what is the simplest way to to get it to start iterating at index 2 and stop iterating at index 9?

Alternatively, is there something from itertools that is more suitable for this. I am specifically interested in high performance methods.

In addition, when the start option was introduced in 2.6, is there any reason why a stop option was not?

Cheers

Advertisement

Answer

JavaScript

or

JavaScript

to rephrase the other suggestion (note that it will only work if your iterable is a sliceable object)

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement