import itertools


def backfill_heights(aliases):
    """
    ZP only started recording heights on ~18th Feb, dates before that have
    0s for height. Backfill these with the last value.

    Example:
        >>> backfill_heights([{'height': 10}, {'height': 9}, {'height': 0},
        ...                     {'height': 10}, {'height': 0}, {'height': 0}])
        [{'height': 10}, {'height': 9}, {'height': 0}, {'height': 10}, {'height': 10}, {'height': 10}]
        >>> backfill_heights([])
        []
    """
    try:
        first_height = next(
            a['height'] for a in aliases[::-1] if a['height'] != 0)
    except StopIteration:
        # People with no aliases, and people who've not changed their profile
        # since height was recorded. We'll just keep the 0s and no deltas will
        # be found.
        return aliases

    backfilled = [
        {**a, 'height': first_height} for a in
        itertools.takewhile(lambda a: a['height'] == 0, aliases[::-1])][::-1]

    default = list(itertools.dropwhile(lambda a: a['height'] == 0,
                                       aliases[::-1]))[::-1]

    return default + backfilled