{"id":25579,"date":"2021-11-23T08:55:20","date_gmt":"2021-11-23T03:25:20","guid":{"rendered":"https:\/\/python-programs.com\/?p=25579"},"modified":"2021-11-23T08:55:20","modified_gmt":"2021-11-23T03:25:20","slug":"python-itertools-dropwhile-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-itertools-dropwhile-function-with-examples\/","title":{"rendered":"Python Itertools.dropwhile() 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.dropwhile() Function:<\/strong><\/p>\n Only after the func. in argument returns false for the first time does Python’s dropwhile() function return an iterator.<\/p>\n Syntax:<\/strong><\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/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 Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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.dropwhile() Function: Only after the func. in argument returns false for the first time does Python’s dropwhile() function return an iterator. Syntax: dropwhile(function, sequence) …<\/p>\ndropwhile(function, sequence)<\/pre>\n
Given List = [6, 7, -3, -1, 4]<\/pre>\n
[-3, -1, 4]<\/pre>\n
Here it removes the positive numbers 6, 7 and the condition becomes false \r\nwhen the number the number is -3. so, the list remains unchanged afterwards.<\/pre>\n
Given List = [3, 6, 8, -4, -9, 10, 56]<\/pre>\n
[-4, -9, 10, 56]<\/pre>\n
Itertools.dropwhile() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import itertools module using the import keyword.\r\nimport itertools\r\n\r\n# Create a function say checkpositive which accepts the number as argument and\r\n# returns True if the number is greater than 0.\r\n\r\n\r\ndef checkpositive(num):\r\n # Return True if the given argument is greater than 0\r\n return num > 0\r\n\r\n\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [6, 7, -3, -1, 4]\r\n# Pass the above function and given list as the arguments to the\r\n# itertools.dropwhile() function and store it in a variable.\r\nrslt = itertools.dropwhile(checkpositive, gvn_lst)\r\n# Convert the above result into a list using the list() function and store it\r\n# in another variable.\r\nrslt_lst = list(rslt)\r\n# Print the above result.\r\nprint(rslt_lst)\r\n<\/pre>\n
[-3, -1, 4]<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import itertools module using the import keyword.\r\nimport itertools\r\n\r\n# Create a function say checkpositive which accepts the number as argument and\r\n# returns True if the number is greater than 0.\r\n\r\n\r\ndef checkpositive(num):\r\n # Return True if the given argument is greater than 0\r\n return num > 0\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# Pass the above function and given list as the arguments to the\r\n# itertools.dropwhile() function and store it in a variable.\r\nrslt = itertools.dropwhile(checkpositive, gvn_lst)\r\n# Convert the above result into a list using the list() function and store it\r\n# in 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 = 3 6 8 -4 -9 10 56\r\n[-4, -9, 10, 56]<\/pre>\n","protected":false},"excerpt":{"rendered":"