And dropping is to discard some things we don’t care about from the beginning of a series, and get only the remainder.
takewhile are both ways of taking, and
dropwhile are both ways of dropping. The difference is how they determine how much of the series to take or drop.
Here we take characters from a string while they are lower-case. The resulting iterator stops at the first place it encounters an upper-case letter.
Haskell’s function is also called
Prelude (though note the capital
takewhile also has a companion named
itertools.dropwhile which removes elements matching the predicate from the beginning of the list and returns the remainder.
Here we drop characters from the string while the characters are lower case. The result we get is the rest of the string starting from the first upper-case letter.
Again, the Haskell function is the same.
span: Taking and dropping at the same time
dropWhile are opposites;
takeWhile takes the part of the list that
dropWhile drops. It is not uncommon to want to perform both of these actions at once, thereby splitting a list into two parts:
This is what Haskell’s
Prelude does: it combines
dropWhile into a single function that gives both results.