[Tutor] Ways of removing consequtive duplicates from a list
avi.e.gross at gmail.com
avi.e.gross at gmail.com
Sun Jul 17 14:02:23 EDT 2022
I was thinking of how expensive it is to push a copy of the first item in
front of a list to avoid special casing in this case.
I mean convert [first, ...] to [first, first, ...]
That neatly can deal with some algorithms such as say calculating a moving
average if you want the first N items to simply be the same as the start
item or a pre-calculated mean of he cumulative sum to that point, rather
than empty or an error.
But I realized that with the python emphasis on iterables in python, there
probably is no easy way to push items into a sort of queue. Maybe you can
somewhat do it with a decorator that intercepts your first calls by
supplying the reserved content and only afterwards calls the iterator. But
as there are so many reasonable solutions, not an avenue needed to explore.
-----Original Message-----
From: Tutor <tutor-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of
Peter Otten
Sent: Sunday, July 17, 2022 4:27 AM
To: tutor at python.org
Subject: Re: [Tutor] Ways of removing consequtive duplicates from a list
On 17/07/2022 00:01, Alex Kleider wrote:
> PS My (at least for me easier to comprehend) solution:
>
> def rm_duplicates(iterable):
> last = ''
> for item in iterable:
> if item != last:
> yield item
> last = item
The problem with this is the choice of the initial value for 'last':
>>> list(rm_duplicates(["", "", 42, "a", "a", ""]))
[42, 'a', ''] # oops, we lost the initial empty string
Manprit avoided that in his similar solution by using a special value that
will compare false except in pathological cases:
> val = object()
> [(val := ele) for ele in lst if ele != val]
Another fix is to yield the first item unconditionally:
def rm_duplicates(iterable):
it = iter(iterable)
try:
last = next(it)
except StopIteration:
return
yield last
for item in it:
if item != last:
yield item
last = item
If you think that this doesn't look very elegant you may join me in the
https://peps.python.org/pep-0479/ haters' club ;)
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list