Skipping the first part of an iterable in Python
Redowan Delowar
February 12, 2023
Consider this iterable:
Let's say you want to build another iterable that includes only the numbers that appear
starting from the element 0. Usually, I'd do this:
While this is quite terse and does the job, it won't work with a generator. There's an even
more generic and terser way to do the same thing with itertools.dropwhile function. Here's
how to do it:
Here, itertools.dropwhile is a generator function that returns elements from an iterable
starting from the first element for which the predicate returns False. The predicate is a
function that takes one argument and returns a boolean value.
The dropwhile function takes two arguments:
- A function (the predicate), which takes one argument and returns a boolean value.
- An iterable, which can be any object that can be iterated over, such as a list, tuple,
string, or even another generator.
The dropwhile function starts iterating over the elements of the iterable, and drops the
elements for which the predicate returns True. It then returns all the remaining elements
of the iterable, regardless of whether they satisfy the condition or not.
Apart from being concise, this implementation is more generic and can be used for other
purposes like skipping the header lines in a file. For example:
This will print all the lines from the /etc/passwd file after the header comments:
Finally, let's see how you can skip straight to the data rows in a CSV file that contains
arbitrary comments and headers like this:
Running this will give you the dicts containing the data rows only:
Further reading
- [Python Cookbook - David Beazley, Ch 4: Iterators and Generators]
- [itertools.dropwhile]
[python cookbook - david beazley, ch 4: iterators and generators]:
https://www.oreilly.com/library/view/python-cookbook-3rd/9781449357337/
[itertools.dropwhile]:
https://docs.python.org/3/library/itertools.html#itertools.dropwhile
Discussion in the ATmosphere