I’m looking for a clean, Pythonic, way to eliminate from the following list:
li = [0, 1, 2, 3, 3, 4, 3, 2, 2, 2, 1, 0, 0]
all contiguous repeated elements (runs longer than one number) so as to obtain:
re = [0, 1, 2, 4, 3, 1]
but although I have working code, it feels un-Pythonic and I am quite sure there must be a way out there (maybe some lesser known itertools functions?) to achieve what I want in a far more concise and elegant way.
Here is a version based on Karl’s answer which doesn’t requires copies of the list (
tmp, the slices, and the zipped list).izipis significantly faster than (Python 2)zipfor large lists.chainis slightly slower than slicing but doesn’t require atmpobject or copies of the list.isliceplus making atmpis a bit faster, but requires more memory and is less elegant.A
timeittest shows it to be approximately twice as fast as Karl’s answer or my fastestgroupbyversion for short groups.Make sure to use a value other than
None(likeobject()) if your list can containNones.Use this version if you need it to work on an iterator / iterable that isn’t a sequence, or your groups are long:
timeitshows it’s about ten times faster than the other version for 1,000 item groups.Earlier, slow versions: