Python – Itertools.dropwhile()

Itertools is a Python module that provide various functions that work on iterators to produce complex iterators. It makes the code faster, memory efficient and thus we see a better performance. This module is either used by themselves or in combination to form iterator algebra.

Note: For more information, refer to Python Itertools

Dropwhile()

The dropwhile() function of Python returns an iterator only after the func. in argument returns false for the first time.



Syntax:

dropwhile(func, seq):

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate the working of   
# dropwhile() 
  
  
# Function to be passed
# as an argument
def is_positive(n):
    return n > 0 
  
value_list =[5, 6, -8, -4, 2]
result = list(itertools.dropwhile(is_positive, value_list)) 
   
print(result) 

chevron_right


Output:

[-8, -4, 2]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate the working of   
# dropwhile() 
    
    
import itertools 
    
    
# initializing list   
li = [2, 4, 5, 7, 8]  
      
# using dropwhile() to start displaying after condition is false  
print ("The values after condition returns false : ", end ="")  
print (list(itertools.dropwhile(lambda x : x % 2 == 0, li))) 

chevron_right


Output:

The values after condition returns false : [5, 7, 8]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.