tuples vs lists

W

worzel

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
 
B

Bruno Desthuilliers

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
 
S

Steve Horsley

worzel said:
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
 
W

worzel

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

'Two-Pull' it is then, thanks.
 
S

Steve Holden

worzel said:
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
 
I

Irmen de Jong

Steve said:
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
 
S

Sean Dolan

worzel said:
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.
 
W

worzel

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'.
 
G

Gerrit

Steve said:
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
 
A

Antoon Pardon

Op 2005-01-08 said:
worzel a écrit :

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.
 
B

Bruno Desthuilliers

Antoon Pardon a écrit :
This is not true.

Chapter and verse, please ?
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
 
A

Antoon Pardon

Op 2005-01-10 said:
Antoon Pardon a écrit :



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.
 
R

Reinhold Birkenfeld

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

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
 
A

Antoon Pardon

Op 2005-01-11 said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top