[Tutor] Duplicate items in list

navin ray navinray91 at gmail.com
Sat Oct 10 00:41:30 EDT 2020


Hi Manprit

It's been a pleasure and a great learning to follow you and others on this
mailing list. I am particularly interested in this problem because this was
recently asked in interview which I wasn't able to answer. Could you
provide solution to the same problem using very simple code (I.e without
using any built in functions) so that even a noob like me could understand.
I prefer codes written using simple logic (even though they are longer),
they are easier to understand.
I am sorry if my suggestions is not relevant to the mailing list.
My apologies in advance.

Regards//


On Sat, 10 Oct 2020, 08:40 Manprit Singh, <manpritsinghece at gmail.com> wrote:

> Dear sir ,
>
> I would like to see suggestions for the solution of the problem given in my
> last mail without using any python standard library module .Is it possible
> ?
>
> Regards
> Manprit Singh
>
> On Fri, Oct 9, 2020 at 9:07 PM Manprit Singh <manpritsinghece at gmail.com>
> 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.
> >
> > Regards
> > Manprit singh
> >
> >
> >
> _______________________________________________
> 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