Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Last active July 7, 2020 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/b2d9559073f64c06d2337ee8e5fe07e2 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/b2d9559073f64c06d2337ee8e5fe07e2 to your computer and use it in GitHub Desktop.
import itertools
#if only one argument is mentioned other than iterable,it is stop value.
l1=itertools.islice([1,2,3,4,5,6,7,8,9,10],5)
print (list(l1))#Output:[1, 2, 3, 4, 5]
#start=2 and stop=5 mentioned. It will start from second index and ends at fifth index
l2=itertools.islice([1,2,3,4,5,6,7,8,9,10],2,5)
print (list(l2))#Output:[3,4,5]
#start=2,step=3.It will start from second index and increment by 3.
l3=itertools.islice([1,2,3,4,5,6,7,8,9,10],2,None,3)
print (list(l3))#Output:[3,6,9]
#step is given as negative value,it raises ValueError
l4=itertools.islice([1,2,3,4,5,6,7,8,9,10],2,None,-2)
print (list(l4))#Output:ValueError: Step for islice() must be a positive integer or None.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment