Not understanding itertools.dropwhile()

Terry Reedy tjreedy at udel.edu
Sat Apr 29 21:00:15 EDT 2017


On 4/29/2017 7:41 PM, Jason Friedman wrote:
> <---- start code ---->
> 
> import itertools
> 
> data = """Line1
> Line2
> 
> Line4
> Line5"""
> 
> def test_to_start(s):
>      return "2" in s
> 
> for line in itertools.dropwhile(test_to_start, data.splitlines()):
>      print(line)
> 
> <---- end code ---->
> 
> I expect:
> 
> $ python3 dropwhile.py
> Line2
> 
> Line4
> Line5
> 
> 
> I get:
> 
> $ python3 dropwhile.py
> Line1
> Line2
> 
> Line4
> Line5
> 
> 
> Please explain.

'2' is not in the first line, so 'dropping' stops.  Read the equivalent 
generator code in the doc.


-- 
Terry Jan Reedy



More information about the Python-list mailing list