Python – Itertools.islice()
In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is islice().
Note: For more information, refer to Python Itertools
This iterator selectively prints the values mentioned in its iterable container passed as an argument.
islice(iterable, start, stop, step)
0 1 2 3 4 [4, 7, 10]
1 2 3 4
Here we have provide the three-parameter that is
range(), 1 and 5. So the first parameter that is iterable as range and second parameter 1 will be considered as start value and 5 will be considered as stop value.
Here we provide the four-parameter that is
range() as iterable, 1, 5 and 2 as stop value. So the first parameter that is iterable as range and second parameter 1 will be considered as start value and 5 will be considered as stop value and 2 will be considered as step value of how many steps to skip while iterating values.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course