On Sat, Oct 24, 2009 at 1:32 PM, bhaskar jain <[email protected]>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
> [email protected]
> http://mail.python.org/mailman/listinfo/bangpypers
>
--
--------------------------------------------------------
blog: http://blog.dhananjaynene.com
twitter: http://twitter.com/dnene http://twitter.com/_pythonic
_______________________________________________
BangPypers mailing list
[email protected]
http://mail.python.org/mailman/listinfo/bangpypers