[Tutor] Duplicate items in list

Manprit Singh manpritsinghece at gmail.com
Sat Oct 10 06:37:21 EDT 2020


Dear Alan Sir,

Your words always teach me something . This time too I got a  lesson from
you.

Regards
Manprit Singh

On Sat, Oct 10, 2020 at 4:00 PM Alan Gauld via Tutor <tutor at python.org>
wrote:

> On 10/10/2020 06:31, Manprit Singh wrote:
>
> > Kindly suggest , if something can be done better:
> >
> > def remove_cons(lst):
> >     for i in range(len(lst)-1):
> >         if lst[i] != lst[i+1]:
> >             yield lst[i]
> >     yield lst[-1]
>
> Any time you find yourself using indices to access
> data in a Python for loop you can usually find
> something better. It's nearly always the wrong thing
> to do.
>
> See Dennis' solution for a much cleaner option
> that uses Python idioms.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> 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