converting a set into a sorted list

Terry Reedy tjreedy at udel.edu
Sat May 14 23:07:16 EDT 2005


"MackS" <mackstevenson at hotmail.com> wrote in message 
news:1116120701.520116.315230 at f14g2000cwb.googlegroups.com...
> Can I somehow avoid doing this in two stages? Can I somehow avoid first
> creating a long list only to immediately sort it afterwards?

Yes, you could interatively extract and append the min of the set 
(selection sort), but this would be O(n**2), like bubble or insert sort, 
instead of the O(n*logn) of make list and sort.  You can do the latter in 
two statements without append:
 display = list(big_set)
 display.sort().

As Robert noted, this can be condensed to one using sorted, but that will 
do the same as the two lines above.

Terry J. Reedy






More information about the Python-list mailing list