On Thu, Nov 10, 2011 at 12:40 PM, lina <lina.lastn...@gmail.com> wrote:
> Hi, > > How to remove the coming duplication, > > Here I wrote one (not working): > > > >>> a=['2', '5', '7', '5', '5'] > >>> for i in range(len(a)-1): > if a[i+1]==a[i]: > a.remove(a[i+1]) > if i not in range(len(a)): > break > > > >>> a > ['2', '7', '5', '5'] > > I wish to get a is [2,5,7,5] > Did you mean [2, 5, 7] just remove the coming duplication, not unique the list. > > Thanks for any advice, > I am assuming that you want to remove ALL duplicates. Option 1: Look at a different data structure -- one that does not have duplicates by design: namely sets Option 2: Sort the list and then remove duplicates as you did. look at sorted(a) Option 3: This is probably the least efficient but has the merit of retaining the original order Count the occurrence of each item and remove 1 less look at a.count() I have chosen to indicate the directions; hope it helps Asokan Pichai Enjoy programming in python
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor