Help understanding the decisions *behind* python?

Duncan Booth duncan.booth at invalid.invalid
Mon Jul 20 13:08:38 EDT 2009


Phillip B Oldham <phillip.oldham at gmail.com> wrote:

> This make a lot more sense to us, and follows the convention from
> other languages. It would also mean chaining methods to manipulate
> lists would be easier:
> 
>>>> x = [2,1,3]
>>>> print x.sort()[0]
> 3
>>>> print x
> [2,1,3]

You already have a way to do what you want:

>>> x = [2,1,3]
>>> print sorted(x)[0]
3
>>> print x
[2,1,3]

as a bonus you can use 'sorted' to sort any sequence including generators 
or tuples, but the result will always be a list: if it was a list method 
then you would have to convert the sequence to a list first.

The main reason why you need both lists and tuples is that because a tuple 
of immutable objects is itself immutable you can use it as a dictionary 
key. You can't use a list as a dictionary key because if something were to 
mutate a key the dictionary structure would behave very strangely indeed.
The types 'set' and 'frozenset' both exist for the same reason.



More information about the Python-list mailing list