[lang-ref] ( example_trim_trailing_empty_elements ) ( python )

def test_example_trim_trailing_empty_elements():
    # not functional
    items = ['A', '', 'B', '', '', 'C', '', '', '']
    expected = ['A', '', 'B', '', '', 'C']

    while len(items) > 0 and items[-1] == '':
        items.pop()

    assert items == expected
def test_example_trim_trailing_empty_elements_alternative():
    # functional
    # It’s possible, but it ends up being harder to read.
    import itertools
    items = ['A', '', 'B', '', '', 'C', '', '', '']
    expected = ['A', '', 'B', '', '', 'C']

    r = list(reversed(list(itertools.dropwhile(lambda x: x == '', reversed(items)))))

    assert r == expected