[Tutor] Duplicate items in list

Manprit Singh manpritsinghece at gmail.com
Fri Oct 9 12:41:13 EDT 2020


Dear Mats Wichmann,

Here in this question my purpose is not to remove duplicates from the
list.This question is about removing duplicates from the groups.
see  the list is lst = [3, 3, 5, 5, 5, 6, 6, 6, 5, 9, 3, 3, 3, 5, 5], the
output i need is :
3
5
6
5
9
3
5
As you can see 3 and 5 are still repeating in the output ,  there are 2
groups of 3 in the list, i have to write a program that gives a single
three for each group of 3, in that way 3 will occur in the output  for 2
times, for the same reason 5 will occur in the output for 3 times .

Regards
Manprit Singh


On Fri, Oct 9, 2020 at 9:47 PM Mats Wichmann <mats at wichmann.us> wrote:

> On 10/9/20 9:37 AM, Manprit Singh wrote:
> > Dear sir,
> >
> > Consider a list as given below :
> >
> > lst = [3, 3, 5, 5, 5, 6, 6, 6, 5, 9, 3, 3, 3, 5, 5]
> > I need to print the values  as given below  and in same order:
> >
> > 3       # print 3 only once as there are two occurrence of 3 in the
> > beginning  in list
> > 5       # print 5 only once as there are 3 occurrence of 5 after 3 in the
> > list
> > 6       # print 6 only once as there are 3 occurrence of 6 after 5 in the
> > list
> > 5       # print 5 only once as there is single occurrence of 5 after 6 in
> > the list
> > 9       # print 9 only once as there is single occurrence of 9 after 5 in
> > the list
> > 3       # print 3 only once as there  are 3 occurrence of 3 after 9 in
> the
> > list
> > 5       # print 5 only once as there are 2 occurrence of 5 in the last
> >
> > I have written the code as given below:
> > lst = [3, 3, 5, 5, 5, 6, 6, 6, 5, 9, 3, 3, 3, 5, 5]
> > for i, j in enumerate(lst[:-1]):
> >     if lst[i+1] != j:
> >         print(j)
> > print(lst[-1])
> >
> > which gives the answer as given above
> > I feel that a better code can be written for this problem , Need your
> > guidance.
>
> Turns out there are a ton of ways to get the unique values from a list.
>  The problem comes up often, usually in the context of "what is the
> fastest way...".  The answer depends on whether you need to preserve the
> order or not, and what degree of cleverness feels comfortable in your code.
>
>  If order doesn't matter, using a set will give you unique
>
> uniques = set(lst)
> # and to print:
> print(*uniques)
>
> If order does matter - as you have indicated it does, it is now possible
> to do with with a dictionary operation. This wins for speed, but isn't
> necessarily the "most obvious":
>
> uniques = list(dict.fromkeys(lst))
>
> dict.fromkeys constructs (using dict's alternate constructor) a
> dictionary given a list of keys (the values will all be None).  When you
> then convert the dictionary to a list, that is implicitly like doing
> list(d.keys()), but you don't have to specify the keys(). Since
> dictionary keys by definition must be unique, this uniquifies.
>
> Other options exist, as noted...
>
> One is to use a set, but only as a tracking devices:
>
> seen = set()
> uniques = [x for x in lst if x not in seen and not seen.add(x)]
> _______________________________________________
> 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