Best way to find unique items in a list
bjorn
bjorn at roguewave.com
Thu Feb 24 15:57:13 EST 2000
Timothy Grant wrote:
> maxm wrote:
> > Anyone for a little brain gym?
> >
> > ------------------------------------------
> >
> > def Unique(theList):
> > uniqueList = []
> > OldValue = ''
> > theList.sort()
> > for value in theList:
> > if OldValue != value:
> > uniqueList.append(value)
> > OldValue = value
> > return uniqueList
> >
> > print Unique(['Max','Gitte','Magnus','Caroline','Clara','Max','Gitte'])
>
> How about...
>
> def Unique(theList):
> uniqueList = []
> for value in theList:
> if value in uniqueList:
> continue
> uniqueList.append(value)
> return uniqueList
shorter and faster... :-)
def Unique(theList):
uniqueList = {}
for value in theList:
uniqueList[value] = 1
return uniqueList.keys()
-- bjorn
More information about the Python-list
mailing list