[Tutor] to sort

Mark Tolonen metolone+gmane at gmail.com
Fri Dec 26 08:27:31 CET 2008


"prasad rao" <prasadaraon50 at gmail.com> wrote in message 
news:9e3fac840812252221pe3345bam7e3e3563e050c940 at mail.gmail.com...
> hello
>        I am trying to sort a list(I know there is a builtin sort method).
>
>
>      a=[21,56,35,47,94,12]
>       b=[]
>
>
>    for x in a:
> b.append (min(a))
> a.remove (min(a))
> >>> a
> [56, 47, 94]
> >>> b
> [12, 21, 35]
>
> It is not Compleating .Doing only 3 rounds.Why?

You should not change a list while iterating over it.  Instead, iterate 
until a is empty:

>>> a=[21,56,35,47,94,12]
>>> b=[]
>>> while a:
...        b.append(min(a))
...        a.remove(min(a))
...
>>> a
[]
>>> b
[12, 21, 35, 47, 56, 94]
>>>

-Mark




More information about the Tutor mailing list