[Tutor] Sort a Set

Terry Carroll carroll at tjc.com
Tue Aug 23 19:47:54 CEST 2005


On Tue, 23 Aug 2005, Terry Carroll wrote to Jonas:

> I don't know if you're in a position to rely on the sortedness of the 
> input data, but even if not, this works:
> 
> >>> l=[24, 24, 15, 16, 16, 15, 24]
> >>> l=sorted(list(set(l)), reverse=True)
> >>> l
> [24, 16, 15]

Sorry, I missed you were on 2.3.x, and I think sorted() is new with 2.4.  
You'd instead have to do the sort in a separate step:

>>> l=[24, 24, 15, 16, 16, 15, 24]
>>> l=list(set(l))
>>> l.sort(reverse=True)
>>> l
[24, 16, 15]



More information about the Tutor mailing list