Radix sort algorithm in Python question

Fredrik Lundh fredrik at effbot.org
Wed Feb 7 03:22:50 EST 2001


Ray Van Dolson wrote:
> del a

you don't have to delete a name that you're about to
reuse on the next line:

(note that variables are name to value mappings,
not values.  deleting a name doesn't always make
the value go away, and binding the name to some-
thing else doesn't affect the original value)

> a=[]
> for section in bins:
>    if len(section) > 0:

if the section is empty, the for loop won't do anything
anyway.  you can get rid of that if statement.

>        for times in range(0,len(section)):
>            a.append(section.pop(0))

the start value to range is optional; range(len(section))
does the same thing.  and since you're looping over the
indices, why not *use* them:

        for index in range(len(section)):
            a.append(section[i])

(pop modifies the section, indexing doesn't)

or better, loop over the items instead:

        for item in section:
            a.append(item)

or better, use the extend method to add the whole
section to the end of the a array:

        a.extend(section)

in 2.0 and later, you can write this as:

        a += section

Cheers /F





More information about the Python-list mailing list