Python Forum
Need help with itertools.islice()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with itertools.islice()
#1
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.
Reply
#2
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.
Reply
#3
Thanks.
Missed the two argument declaration.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cant use difflib with islice? gonksoup 2 313 Jan-22-2024, 01:07 PM
Last Post: deanhystad
  itertools and amazing speed Pedroski55 8 1,985 Nov-11-2022, 01:20 PM
Last Post: Gribouillis
  What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? Pedroski55 3 2,356 Nov-29-2020, 08:35 AM
Last Post: DeaD_EyE
  Making lists using itertools and eliminating duplicates. mike3891 2 2,190 Oct-26-2020, 05:39 PM
Last Post: bowlofred
  Python3 itertools.groupby printing the key august 1 2,046 Aug-17-2020, 05:46 AM
Last Post: bowlofred
  Generate Cartesian Products with Itertools Incrementally CoderMan 2 1,811 Jun-04-2020, 04:51 PM
Last Post: CoderMan
  itertools.zip_shortest() fo unequal iterators Skaperen 10 6,581 Dec-27-2019, 12:17 AM
Last Post: Skaperen
  can itertools compact a list removing all of some value? Skaperen 6 3,102 Sep-02-2019, 03:19 AM
Last Post: Skaperen
  Help with itertools jarrod0987 1 1,782 Jun-10-2019, 02:41 AM
Last Post: Larz60+
  ImportError: No module named jaraco.itertools in Python manhnt 0 2,940 Nov-08-2018, 11:41 AM
Last Post: manhnt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020