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)
Examples:
Example1:
Input:
Given List = [6, 7, -3, -1, 4]
Output:
[-3, -1, 4]
Explanation:
Here it removes the positive numbers 6, 7 and the condition becomes false when the number the number is -3. so, the list remains unchanged afterwards.
Example2:
Input:
Given List = [3, 6, 8, -4, -9, 10, 56]
Output:
[-4, -9, 10, 56]
Itertools.dropwhile() Function with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import itertools module using the import keyword.
- Create a function say checkpositive which accepts the number as argument and returns True if the number is greater than 0.
- Inside the function, return True if the given argument is greater than 0.
- Give the list as static input and store it in a variable.
- Pass the above function and given list as the arguments to the itertools.dropwhile()Â function and store it in a 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 itertools module using the import keyword. import itertools # Create a function say checkpositive which accepts the number as argument and # returns True if the number is greater than 0. def checkpositive(num): # Return True if the given argument is greater than 0 return num > 0 # Give the list as static input and store it in a variable. gvn_lst = [6, 7, -3, -1, 4] # Pass the above function and given list as the arguments to the # itertools.dropwhile() function and store it in a variable. rslt = itertools.dropwhile(checkpositive, gvn_lst) # 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:
[-3, -1, 4]
Method #2: Using Built-in Functions (User Input)
Approach:
- Import itertools module using the import keyword.
- Create a function say checkpositive which accepts the number as argument and returns True if the number is greater than 0.
- Inside the function, return True if the given argument is greater than 0.
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Pass the above function and given list as the arguments to the itertools.dropwhile()Â function and store it in a 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 itertools module using the import keyword. import itertools # Create a function say checkpositive which accepts the number as argument and # returns True if the number is greater than 0. def checkpositive(num): # Return True if the given argument is greater than 0 return num > 0 # 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())) # Pass the above function and given list as the arguments to the # itertools.dropwhile() function and store it in a variable. rslt = itertools.dropwhile(checkpositive, gvn_lst) # 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 = 3 6 8 -4 -9 10 56 [-4, -9, 10, 56]