[BangPypers] sort query

Dhananjay Nene dhananjay.nene at gmail.com
Sat Oct 24 10:41:35 CEST 2009


On Sat, Oct 24, 2009 at 1:32 PM, bhaskar jain <bhaskar.jain2002 at gmail.com>wrote:

> Hello,
>
>  Can sort not modify read-only location.
>
> >>> d
> {'a': 1, 'c': 3, 'b': 2}
>
> >>> id(d)
> 412816
>
> >>> id(d.keys())
> 404296
>
> >>> type(d.keys())
> <type 'list'>
>
> >>> print d.keys().sort()
> None
>
>
> We can so sorted(d.keys()) and it works but was just wondering whether sort
> which modifies in-place fails when the location is read-only.
>

>>> x = d.keys()
>>> print x
['a', 'c', 'b']
>>> x.sort()
>>> print x
['a', 'b', 'c']

The sort method modifies the list in place but doesn't return the sorted
list (ie. returns None)
However it does what it is meant to do as based on the further example
above.

>From http://docs.python.org/library/stdtypes.html

"The sort() and reverse() methods modify the list in place for economy of
space when sorting or reversing a large list. *To remind you that they
operate by side effect*, they don’t return the sorted or reversed list."

Cheers,
Dhananjay

>
> Thanks,
>  Bhaskar.
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
--------------------------------------------------------
blog: http://blog.dhananjaynene.com
twitter: http://twitter.com/dnene http://twitter.com/_pythonic


More information about the BangPypers mailing list