{"id":25584,"date":"2021-11-23T08:55:02","date_gmt":"2021-11-23T03:25:02","guid":{"rendered":"https:\/\/python-programs.com\/?p=25584"},"modified":"2021-11-23T08:55:02","modified_gmt":"2021-11-23T03:25:02","slug":"python-itertools-islice-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-itertools-islice-function-with-examples\/","title":{"rendered":"Python Itertools.islice() Function with Examples"},"content":{"rendered":"
Itertools Module:<\/strong><\/p>\n 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.<\/p>\n Itertools.islice() Function:<\/strong><\/p>\n This iterator prints only the values specified in the iterable container passed as an argument.<\/p>\n Syntax:<\/strong><\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Similarly, try for the other examples<\/strong><\/p>\n Here we are giving range, start and stop values as arguments to the islice() function<\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" 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: …<\/p>\nislice(iterable, start, stop, step)<\/pre>\n
Given list = [10, 20, 30, 40]\r\nGiven start = 2\r\nGiven stop = 25\r\nGiven step = 3<\/pre>\n
[30]<\/pre>\n
Given list = [2, 5, 6, 7, 8]\r\nGiven start = 3\r\nGiven stop = 20\r\nGiven step = 3<\/pre>\n
[7]<\/pre>\n
Itertools.islice() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import islice() function from itertools using the import keyword.\r\nfrom itertools import islice\r\n\r\n\r\n# Loop using the islice function and for loop by passing the arguments as range\r\n# and stepvalue.\r\nfor itr in islice(range(15), 2):\r\n # Inside the loop, print the iterator value.\r\n print(itr)\r\n\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [1, 4, 2, 3, 8, 9]\r\n# Use the islice() function by passing the arguments given list, start, end, and\r\n# step values for slicing the given list.\r\n# Store it in another variable.\r\nrslt = islice(gvn_lst, 2, 5, 2)\r\n# Convert the above result into a list using the list() function and store it in\r\n# another variable.\r\nrslt_lst = list(rslt)\r\n# Print the above result.\r\nprint(rslt_lst)\r\n<\/pre>\n
0\r\n1\r\n[2, 8]<\/pre>\n
# Import islice() function from itertools using the import keyword.\r\nfrom itertools import islice\r\n\r\n# Loop using the islice function and for loop by passing the arguments as range\r\n# start, stop values.\r\nfor itr in islice(range(23), 2, 10):\r\n print(itr)\r\n<\/pre>\n
2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import islice() function from itertools using the import keyword.\r\nfrom itertools import islice\r\n\r\n\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the start value as user input using the int(input()) function and store it in a variable.\r\ngvn_strt = int(input(\"Enter some random number = \"))\r\n# Give the stop value as user input using the int(input()) function and store it in another variable.\r\ngvn_stop = int(input(\"Enter some random number = \"))\r\n# Give the step value as user input using the int(input()) function and store it in another variable.\r\ngvn_step = int(input(\"Enter some random number = \"))\r\n# Use the islice() function by passing the arguments given list, start, end, and\r\n# step values for slicing the given list.\r\n# Store it in another variable.\r\nrslt = islice(gvn_lst, gvn_strt, gvn_stop, gvn_step)\r\n# Convert the above result into a list using the list() function and store it in\r\n# another variable.\r\nrslt_lst = list(rslt)\r\n# Print the above result.\r\nprint(rslt_lst)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 10 20 30 40\r\nEnter some random number = 2\r\nEnter some random number = 25\r\nEnter some random number = 3\r\n[30]<\/pre>\n