How to make a for loop return filler lines when it can't reach a predetermined number?

Viewed 63

I am scraping n iterations of the same type of data from 'a, b, c, d' sources, but sometimes one of these data sources will return less than n values because it does not have that much to pull from (for example I will set limit=5, but c will only have 3 values to scrape from).

The problem is that I am organizing the returned data in lists by skipping every nth value, so it messes up the pattern if one of these don't return at least an empty filler line when it cannot pull anything.

1 Answers

You could write a wrapper for the generator that guarantees a result, even if that result is None:

def get_result(source, fillvalue=None):
    return next(source, fillvalue)

>>> iterator = iter([1, 2, 3])
>>> [get_result(iterator) for _ in range(5)]
[1, 2, 3, None, None]

Alternatively, pass your chunk of 5 results through a function that enforces length

def enforce_length(n):
    def wrapped(lst):
        for _ in range(n):
            yield next(lst, None)

    return wrapped

enforce_length_5 = enforce_length(5)

>>> list(enforce_length_5([1, 2, 3]))
[1, 2, 3, None, None]

However it's likely that there's a better solution that could be had by reorganizing your code a little.

Related