list.sort()

William Park opengeometry at yahoo.ca
Sun Jun 17 13:58:22 EDT 2001


On Sun, Jun 17, 2001 at 07:42:09PM +0200, Rikard Bosnjakovic wrote:
> >>> l = l.sort()
> >>> l
> >>> print l
> None
> 
> If we split the problem into parts, "l.sort()" sorts l and it becomes
> [2,3,4]. I expected it to be [2,3,4] after assigning it to itself (after
> it got sorted), but the list seem to got deleted instead. I don't like
> this behaviour, at all.

'l.sort()' returns 'None', and you are assigning this 'None' back to
'l'.  Instead, try
    >>> l.sort()
    >>> print l

> Why does Python behave like this? Why does sort() change lists
> _in-place_, instead of returning a list?

Efficiency.

-- 
William Park, Open Geometry Consulting, <opengeometry at yahoo.ca>
8 CPUs cluster, (Slackware) Linux, Python, LaTeX, Vim, Mutt, Sc.




More information about the Python-list mailing list