tuples vs lists

Discussion in 'Python' started by worzel, Jan 8, 2005.

  1. worzel

    worzel Guest

    I get what the difference is between a tuple and a list, but why would I
    ever care about the tuple's immuutability?
    Also, do you say 'too-ple' or 'chu-ple' - if you get my drift. (tomato or
    tomato kind of thing)
    TIA
     
    worzel, Jan 8, 2005
    #1
    1. Advertising

  2. worzel a écrit :
    > I get what the difference is between a tuple and a list, but why would I
    > ever care about the tuple's immuutability?


    Because, from a purely pratical POV, only an immutable object can be
    used as kay in a dict. So you can use tuples for 'composed key'.

    Bruno
     
    Bruno Desthuilliers, Jan 8, 2005
    #2
    1. Advertising

  3. worzel wrote:
    > I get what the difference is between a tuple and a list, but why would I
    > ever care about the tuple's immuutability?


    Mainly for security and speed. Many library functions return info by returning
    a reference to an internally held tuple, and could be damaged / compromised
    / corrupted if that internal data was modified by malicious code. If tuples
    were mutable (lists) then it would be necessary to return a copy instead.

    > Also, do you say 'too-ple' or 'chu-ple' - if you get my drift. (tomato or
    > tomato kind of thing)


    Try 'Two-pull'.

    Steve
     
    Steve Horsley, Jan 8, 2005
    #3
  4. worzel

    worzel Guest

    Cheers - thanks for the feedback guys - pretty much answers the question for
    me.

    'Two-Pull' it is then, thanks.


    "Steve Horsley" <> wrote in message
    news:croqtg$qo0$2surf.net...
    > worzel wrote:
    >> I get what the difference is between a tuple and a list, but why would I
    >> ever care about the tuple's immuutability?

    >
    > Mainly for security and speed. Many library functions return info by
    > returning
    > a reference to an internally held tuple, and could be damaged /
    > compromised
    > / corrupted if that internal data was modified by malicious code. If
    > tuples
    > were mutable (lists) then it would be necessary to return a copy instead.
    >
    >> Also, do you say 'too-ple' or 'chu-ple' - if you get my drift. (tomato or
    >> tomato kind of thing)

    >
    > Try 'Two-pull'.
    >
    > Steve
     
    worzel, Jan 8, 2005
    #4
  5. worzel

    Steve Holden Guest

    worzel wrote:

    > Cheers - thanks for the feedback guys - pretty much answers the question for
    > me.
    >
    > 'Two-Pull' it is then, thanks.
    >

    Well, it might be "Two-Pull" in American, but in English it's "tyoopl"
    -- NOT "choopl" (blearch!). I've also heard people say "tuppl".

    So, basically, say whatever you want. Language is about communication :)

    you-say-tomato-ly y'rs - steve
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/
    Holden Web LLC +1 703 861 4237 +1 800 494 3119
     
    Steve Holden, Jan 8, 2005
    #5
  6. Steve Holden wrote:
    > Well, it might be "Two-Pull" in American, but in English it's "tyoopl"
    > -- NOT "choopl" (blearch!). I've also heard people say "tuppl".


    Probably the same ones who attend Tuppl-ware parties.

    --Irmen
     
    Irmen de Jong, Jan 8, 2005
    #6
  7. On Sat, 8 Jan 2005 21:00:27 +0800, "worzel" <> declaimed
    the following in comp.lang.python:

    > Also, do you say 'too-ple' or 'chu-ple' - if you get my drift. (tomato or
    > tomato kind of thing)


    I probably slaughter the word... tup-ell

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Jan 8, 2005
    #7
  8. worzel

    Sean Dolan Guest

    worzel wrote:
    > I get what the difference is between a tuple and a list, but why would I
    > ever care about the tuple's immuutability?
    > Also, do you say 'too-ple' or 'chu-ple' - if you get my drift. (tomato or
    > tomato kind of thing)
    > TIA
    >
    >

    I use the Festival Speech Synthesis System to learn pronunciations
    sometimes. The American english voice is quite accurate.

    --
    Sean Dolan
     
    Sean Dolan, Jan 9, 2005
    #8
  9. worzel

    worzel Guest

    yes, "tyoopl" - thats what I meant by 'choo-ple' (not v good at the
    phonetics)
    As a scouse git (though living in Australia), I would definitely say
    'tyoopl'.

    "Steve Holden" <> wrote in message
    news:w5WDd.69983$Jk5.66802@lakeread01...
    > worzel wrote:
    >
    >> Cheers - thanks for the feedback guys - pretty much answers the question
    >> for me.
    >>
    >> 'Two-Pull' it is then, thanks.
    >>

    > Well, it might be "Two-Pull" in American, but in English it's "tyoopl" --
    > NOT "choopl" (blearch!). I've also heard people say "tuppl".
    >
    > So, basically, say whatever you want. Language is about communication :)
    >
    > you-say-tomato-ly y'rs - steve
    > --
    > Steve Holden http://www.holdenweb.com/
    > Python Web Programming http://pydish.holdenweb.com/
    > Holden Web LLC +1 703 861 4237 +1 800 494 3119
     
    worzel, Jan 9, 2005
    #9
  10. worzel

    Gerrit Guest

    Steve Holden wrote:
    > worzel wrote:
    > >'Two-Pull' it is then, thanks.
    > >

    > Well, it might be "Two-Pull" in American, but in English it's "tyoopl"
    > -- NOT "choopl" (blearch!). I've also heard people say "tuppl".
    >
    > So, basically, say whatever you want. Language is about communication :)


    Or just write it down and don't say the word at all (-:

    regards,
    Gerrit, who actually says tüpel.

    --
    Weather in Lulea / Kallax, Sweden 10/01 10:20:
    -12.0°C wind 0.0 m/s None (34 m above NAP)
    --
    In the councils of government, we must guard against the acquisition of
    unwarranted influence, whether sought or unsought, by the
    military-industrial complex. The potential for the disastrous rise of
    misplaced power exists and will persist.
    -Dwight David Eisenhower, January 17, 1961
     
    Gerrit, Jan 10, 2005
    #10
  11. Op 2005-01-08, Bruno Desthuilliers schreef <>:
    > worzel a écrit :
    >> I get what the difference is between a tuple and a list, but why would I
    >> ever care about the tuple's immuutability?

    >
    > Because, from a purely pratical POV, only an immutable object can be
    > used as kay in a dict.


    This is not true.

    > So you can use tuples for 'composed key'.


    lists can be so used too. Just provide a hash.

    --
    Antoon Pardon
     
    Antoon Pardon, Jan 10, 2005
    #11
  12. Antoon Pardon a écrit :
    > Op 2005-01-08, Bruno Desthuilliers schreef <>:
    >
    >>worzel a écrit :
    >>
    >>>I get what the difference is between a tuple and a list, but why would I
    >>>ever care about the tuple's immuutability?

    >>
    >>Because, from a purely pratical POV, only an immutable object can be
    >>used as kay in a dict.


    <my-bad> s/kay/key/ </my-bad>

    > This is not true.


    Chapter and verse, please ?

    >
    >>So you can use tuples for 'composed key'.

    >
    > lists can be so used too. Just provide a hash.


    Please show us an example, and let's see how useful and handy this is
    from a "purely practical POV" ?-)

    Bruno
     
    Bruno Desthuilliers, Jan 10, 2005
    #12
  13. Op 2005-01-10, Bruno Desthuilliers schreef <>:
    > Antoon Pardon a écrit :
    >> Op 2005-01-08, Bruno Desthuilliers schreef <>:
    >>
    >>>worzel a écrit :
    >>>
    >>>>I get what the difference is between a tuple and a list, but why would I
    >>>>ever care about the tuple's immuutability?
    >>>
    >>>Because, from a purely pratical POV, only an immutable object can be
    >>>used as kay in a dict.

    >
    ><my-bad> s/kay/key/ </my-bad>
    >
    >> This is not true.

    >
    > Chapter and verse, please ?


    I don't need chapter and verse. I have already used mutable
    objects as keys and it works just fine.

    >>> class hlst(list):
    >>>
    >>> def __hash__(self):
    >>> sum = 0
    >>> for el in self:
    >>> sum += hash(el)
    >>> return sum % 0x37777777
    >>>
    >>> lst = hlst([3,5,7])
    >>> lst

    [3, 5, 7]
    >>> lst[0] = 12
    >>> lst

    [12, 5, 7]
    >>> d = {}
    >>> d[lst] = 4


    >>
    >>>So you can use tuples for 'composed key'.

    >>
    >> lists can be so used too. Just provide a hash.

    >
    > Please show us an example, and let's see how useful and handy this is
    > from a "purely practical POV" ?-)


    It is handy from a pratical point of view when most operations you
    do on your data are the equivalent of appending, deleting and changing
    one element. Simulating these with tuples will cost you more than
    putting a copy of your list in the dictionary as a precaution against
    accidently mutating a key.

    --
    Antoon Pardon
     
    Antoon Pardon, Jan 11, 2005
    #13
  14. Antoon Pardon wrote:
    > Op 2005-01-10, Bruno Desthuilliers schreef <>:
    >> Antoon Pardon a écrit :
    >>> Op 2005-01-08, Bruno Desthuilliers schreef <>:
    >>>
    >>>>worzel a écrit :
    >>>>
    >>>>>I get what the difference is between a tuple and a list, but why would I
    >>>>>ever care about the tuple's immuutability?
    >>>>
    >>>>Because, from a purely pratical POV, only an immutable object can be
    >>>>used as kay in a dict.

    >>
    >><my-bad> s/kay/key/ </my-bad>
    >>
    >>> This is not true.

    >>
    >> Chapter and verse, please ?

    >
    > I don't need chapter and verse. I have already used mutable
    > objects as keys and it works just fine.
    >
    >>>> class hlst(list):
    >>>>
    >>>> def __hash__(self):
    >>>> sum = 0
    >>>> for el in self:
    >>>> sum += hash(el)
    >>>> return sum % 0x37777777
    >>>>


    Given this hash function, how do you handle changed keys?

    ..class hlst(list):
    .. def __hash__(self):
    .. sum = 0
    .. for el in self:
    .. sum += hash(el)
    .. return sum % 0x37777777
    ..
    ..lst = hlst([1,2,3])
    ..
    ..d = {}
    ..d[lst] = 1
    ..
    ..lst[0] = 0
    ..
    ..print d
    ..try:
    .. print d[hlst([0,2,3])]
    ..except KeyError:
    .. print "0,2,3: KeyError"
    ..try:
    .. print d[hlst([1,2,3])]
    ..except KeyError:
    .. print "1,2,3: KeyError"

    raises the KeyError twice. How do you access the element then?

    And if you can't access the element when it's changed, what is the
    advantage over using tuples?

    Reinhold
     
    Reinhold Birkenfeld, Jan 11, 2005
    #14
  15. Op 2005-01-11, Reinhold Birkenfeld schreef <>:
    > Antoon Pardon wrote:
    >> Op 2005-01-10, Bruno Desthuilliers schreef <>:
    >>> Antoon Pardon a écrit :
    >>>> Op 2005-01-08, Bruno Desthuilliers schreef <>:
    >>>>
    >>>>>worzel a écrit :
    >>>>>
    >>>>>>I get what the difference is between a tuple and a list, but why would I
    >>>>>>ever care about the tuple's immuutability?
    >>>>>
    >>>>>Because, from a purely pratical POV, only an immutable object can be
    >>>>>used as kay in a dict.
    >>>
    >>><my-bad> s/kay/key/ </my-bad>
    >>>
    >>>> This is not true.
    >>>
    >>> Chapter and verse, please ?

    >>
    >> I don't need chapter and verse. I have already used mutable
    >> objects as keys and it works just fine.
    >>
    >>>>> class hlst(list):
    >>>>>
    >>>>> def __hash__(self):
    >>>>> sum = 0
    >>>>> for el in self:
    >>>>> sum += hash(el)
    >>>>> return sum % 0x37777777
    >>>>>

    >
    > Given this hash function, how do you handle changed keys?


    I don't change keys. The fact that I like to use a mutable
    as a key doesn't imply I want to mutate a key.

    > And if you can't access the element when it's changed, what is the
    > advantage over using tuples?


    The debate over what the adavantage is of tuples over lists or vice
    versa as keys in dictionaries is IMO misguided. Whether I use a list
    or a tuple is not guided by whether they are going to be used as a
    key or not, but how in general the data is to be manipulated.

    If the typical manipulations are modifications of an existing object,
    I use a list, if the typical manipulation creates new objects out
    of old ones I use a tuple. If I then find that I need this object
    as a key, I just provide a hash so that I can use this object as
    a key in a straight forward manner, without the hassle of converting
    to and from a tuple all the time.

    --
    Antoon Pardon
     
    Antoon Pardon, Jan 12, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Nogradi
    Replies:
    3
    Views:
    368
    Dennis Lee Bieber
    Nov 10, 2006
  2. Replies:
    5
    Views:
    578
    Thomas J. Gritzan
    Oct 6, 2006
  3. tuples within tuples

    , Oct 26, 2007, in forum: Python
    Replies:
    12
    Views:
    611
    Dennis Lee Bieber
    Oct 27, 2007
  4. xera121
    Replies:
    8
    Views:
    759
    lolmc
    Sep 30, 2009
  5. Jon Reyes
    Replies:
    18
    Views:
    271
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page