Python Itertools.islice() Function with Examples

Itertools Module:

Itertools is a Python module that contains a collection of functions for dealing with iterators. They make it very simple to iterate through iterables such as lists and strings.

Itertools.islice() Function:

This iterator prints only the values specified in the iterable container passed as an argument.

Syntax:

islice(iterable, start, stop, step)

Examples:

Example1:

Input:

Given list = [10, 20, 30, 40]
Given start = 2
Given stop =  25
Given step = 3

Output:

[30]

Example2:

Input:

Given list = [2, 5, 6, 7, 8]
Given start = 3
Given stop = 20
Given step = 3

Output:

[7]

Itertools.islice() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import islice() function from itertools using the import keyword.
  • Loop using the islice and for loop by passing the arguments range and stepvalue.
  • Inside the loop, print the iterator value.
  • Give the list as static input and store it in a variable.
  • Use the islice() function by passing the arguments given list, start, end, and step values for slicing the given list.
  • Store it in another variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import islice() function from itertools using the import keyword.
from itertools import islice


# Loop using the islice function and for loop by passing the arguments as range
# and stepvalue.
for itr in islice(range(15), 2):
    # Inside the loop, print the iterator value.
    print(itr)

# Give the list as static input and store it in a variable.
gvn_lst = [1, 4, 2, 3, 8, 9]
# Use the islice() function by passing the arguments given list, start, end, and
# step values for slicing the given list.
# Store it in another variable.
rslt = islice(gvn_lst, 2, 5, 2)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print(rslt_lst)

Output:

0
1
[2, 8]

Similarly, try for the other examples

Here we are giving range, start and stop values as arguments to the islice() function

# Import islice() function from itertools using the import keyword.
from itertools import islice

# Loop using the islice function and for loop by passing the arguments as range
# start, stop values.
for itr in islice(range(23), 2, 10):
    print(itr)

Output:

2
3
4
5
6
7
8
9

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import islice() function from itertools using the import keyword.
  • Loop using the islice and for loop by passing the arguments range and stepvalue.
  • Inside the loop, print the iterator value.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the start value as user input using the int(input()) function and store it in a variable.
  • Give the stop value as user input using the int(input()) function and store it in another variable.
  • Give the step value as user input using the int(input()) function and store it in another variable.
  • Use the islice() function by passing the arguments given list, start, end, and step values for slicing the given list.
  • Store it in another variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import islice() function from itertools using the import keyword.
from itertools import islice


# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the start value as user input using the int(input()) function and store it in a variable.
gvn_strt = int(input("Enter some random number = "))
# Give the stop value as user input using the int(input()) function and store it in another variable.
gvn_stop = int(input("Enter some random number = "))
# Give the step value as user input using the int(input()) function and store it in another variable.
gvn_step = int(input("Enter some random number = "))
# Use the islice() function by passing the arguments given list, start, end, and
# step values for slicing the given list.
# Store it in another variable.
rslt = islice(gvn_lst, gvn_strt, gvn_stop, gvn_step)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print(rslt_lst)

Output:

Enter some random List Elements separated by spaces = 10 20 30 40
Enter some random number = 2
Enter some random number = 25
Enter some random number = 3
[30]