Python Forum

Full Version: Need help with itertools.islice()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I recently came across this function but can't make sense of the arguments.
The documentation gives the declaration as below and examples.
https://docs.python.org/2/library/iterto...ols.islice

itertools.islice(iterable, start, stop[, step])
# islice('ABCDEFG', 2) --> A B
# islice('ABCDEFG', 2, 4) --> C D
Example 1 shows that A B are printed but but the argument '2' is the starting index
so shouldn't the output be CDEFG?

Thanks in advance for any help.
There are two ways to call islice, as shown in the docs:
Quote:itertools.islice(iterable, stop)
itertools.islice(iterable, start, stop[, step])
As you can see, in the two-argument form, the second argument is the stopping index, not the starting one.
Thanks.
Missed the two argument declaration.