Velocity Reviews > Help understanding the decisions *behind* python?

# Help understanding the decisions *behind* python?

Phillip B Oldham
Guest
Posts: n/a

 07-20-2009
My colleagues and I have been working with python for around 6 months
now, and while we love a lot of what python has done for us and what
it enables us to do some of the decisions behind such certain
data-types and their related methods baffle us slightly (when compared
to the decisions made in other, similarly powerful languages).

Specifically the "differences" between lists and tuples have us
confused and have caused many "discussions" in the office. We
understand that lists are mutable and tuples are not, but we're a
little lost as to why the two were kept separate from the start. They
both perform a very similar job as far as we can tell.

Consider the following:

>>> x = [2,1,3]
>>> x.sort()
>>> print x

[1, 2, 3]

Now, if the sort operations were unable to affect the original
structure of the list (as in JavaScript) you'd effectively have a
tuple which you could add/remove from, and the example above would
look more like:

>>> x = [2,1,3]
>>> print x.sort()

[1, 2, 3]
>>> print x

[2,1,3]

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]

We often find we need to do manipulations like the above without
changing the order of the original list, and languages like JS allow
this. We can't work out how to do this in python though, other than
duplicating the list, sorting, reversing, then discarding.

We're not looking to start any arguments or religious wars and we're
not asking that python be changed into something its not. We'd simply
like to understand the decision behind the lists and tuple structures.
We feel that in not "getting" the difference between the two types we
may be missing out on using these data structures to their full
potential.

Anthony Tolle
Guest
Posts: n/a

 07-20-2009
On Jul 20, 12:27*pm, Phillip B Oldham <(E-Mail Removed)>
wrote:
> ...
> Specifically the "differences" between lists and tuples have us
> confused and have caused many "discussions" in the office. We
> understand that lists are mutable and tuples are not, but we're a
> little lost as to why the two were kept separate from the start. They
> both perform a very similar job as far as we can tell.
> ...

There's no hard-set rules, but tuples are typically used to hold
collections of heterogeneous data, e.g. (10, "spam", 3.21). As has
been mentioned, such a tuple can be used as a dictionary key, whereas
a list cannot be used as a dictionary key, because it is mutable.

Lists, on the other hand, typically hold collections of homogeneous
data, e.g. [1, 2, 5] or ["spam", "eggs", "sausage"].

Of course, you'll also see plenty of examples of tuples containing
homogeneous data and lists containing heterogeneous data

Phillip B Oldham
Guest
Posts: n/a

 07-20-2009
On Jul 20, 6:08*pm, Duncan Booth <(E-Mail Removed)> wrote:
> 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.

Really? That sounds interesting, although I can't think of any real-
world cases where you'd use something like that.

J. Cliff Dyer
Guest
Posts: n/a

 07-20-2009
On Mon, 2009-07-20 at 12:26 -0700, Phillip B Oldham wrote:
> On Jul 20, 6:08 pm, Duncan Booth <(E-Mail Removed)> wrote:
> > 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.

>
> Really? That sounds interesting, although I can't think of any real-
> world cases where you'd use something like that.

Well, if you wanted to index a dictionary by coordinates, you might do
something like this:

fleet = {}
fleet[9,4] = 'destroyer'
fleet[8,4] = 'destroyer'
fleet[3,5] = 'aircraftcarrier'
fleet[4,5] = 'aircraftcarrier'
fleet[5,5] = 'aircraftcarrier'
fleet[6,5] = 'aircraftcarrier'
fleet[8,0] = 'battleship'
fleet[8,1] = 'battleship'
fleet[8,2] = 'battleship'

def checkattack(x, y, fleet):
if x,y in fleet:
return "You hit my %s' % fleet[x,y]

Maybe not the best implementation of Battleship, but you get the idea.

Piet van Oostrum
Guest
Posts: n/a

 07-20-2009
>>>>> Duncan Booth <(E-Mail Removed)> (DB) wrote:

>DB> Phillip B Oldham <(E-Mail Removed)> 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]

>DB> You already have a way to do what you want:

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

>DB> 3

What kind of Python produces that?
--
Piet van Oostrum <(E-Mail Removed)>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: http://www.velocityreviews.com/forums/(E-Mail Removed)

Chris Rebert
Guest
Posts: n/a

 07-20-2009
On Mon, Jul 20, 2009 at 2:23 PM, Piet van Oostrum<(E-Mail Removed)> wrote:
>>>>>> Duncan Booth <(E-Mail Removed)> (DB) wrote:

>
>>DB> Phillip B Oldham <(E-Mail Removed)> 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]

>
>>DB> You already have a way to do what you want:

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

>>DB> 3

>
> What kind of Python produces that?

Assuming you're referring to the latter example, it was added in version 2.4
If you meant the former example, I think that's purely pseudo-Python.

Cheers,
Chris
--
http://blog.rebertia.com

Paul Moore
Guest
Posts: n/a

 07-20-2009
2009/7/20 Chris Rebert <(E-Mail Removed)>:
> On Mon, Jul 20, 2009 at 2:23 PM, Piet van Oostrum<(E-Mail Removed)> wrote:
>>>>>>> x = [2,1,3]
>>>>>>> print sorted(x)[0]
>>>DB> 3

>>
>> What kind of Python produces that?

>
> Assuming you're referring to the latter example, it was added in version 2.4
> If you meant the former example, I think that's purely pseudo-Python.

I think he was referring to the fact that the result should be 1, not 3.

Paul.

Hrvoje Niksic
Guest
Posts: n/a

 07-20-2009
Phillip B Oldham <(E-Mail Removed)> writes:

> On Jul 20, 6:08*pm, Duncan Booth <(E-Mail Removed)> wrote:
>> 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.

>
> Really? That sounds interesting, although I can't think of any real-
> world cases where you'd use something like that.

An application visiting files on a filesystem recursively needs a
dictionary or set keyed by (st_dev, st_ino) to make sure it doesn't
visit the same file twice. Memoization implementation (of a cache for
results of function application) might need to use a dictionary to map
function arguments, a tuple, to the function result.

Hrvoje Niksic
Guest
Posts: n/a

 07-20-2009
Chris Rebert <(E-Mail Removed)> writes:

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

>>
>> What kind of Python produces that?

>
> Assuming you're referring to the latter example, it was added in version 2.4
> If you meant the former example, I think that's purely pseudo-Python.

sorted([2, 1, 3])[0] evaluates to 1, not 3.

Niels L. Ellegaard
Guest
Posts: n/a

 07-20-2009
Phillip B Oldham <(E-Mail Removed)> writes:

> We often find we need to do manipulations like the above without
> changing the order of the original list, and languages like JS allow
> this. We can't work out how to do this in python though, other than
> duplicating the list, sorting, reversing, then discarding.

If you just want a one-liner, and you don't care about speed you can
do the following (but I don't think this is considered best practice)

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

[1, 2, 3]
>>> print x

[2, 1, 3]

Niels

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Max Burke NZ Computing 25 09-24-2006 05:58 AM Waterspider Digital Photography 8 12-28-2005 09:48 PM Larry Digital Photography 4 06-28-2005 03:41 PM lollypop3_14@yahoo.com ASP .Net 0 11-11-2004 01:31 PM JTS ASP .Net 0 01-22-2004 07:51 AM

Advertisments