def oddlines(fileobj):
return (line for index,line in enumerate(fileobj) if index % 2)
def evenlines(fileobj):
return (line for index,line in enumerate(fileobj) if not index % 2)
Note that this will require scanning the file twice, since these aren't designed to run in parallel. It does, however, lead to much less complex code. (Also note that an 'odd' line here is one with an index of 1,3,5 - which means that the first line is an 'even' line due to zero-indexing.)
As Ashwini notes, you could also use itertools.islice
to do this.