# tuples vs lists

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Tue Jan 11 11:53:03 EST 2005

```Antoon Pardon wrote:
> Op 2005-01-10, Bruno Desthuilliers schreef <bdesth.quelquechose at free.quelquepart.fr>:
>> Antoon Pardon a écrit :
>>> Op 2005-01-08, Bruno Desthuilliers schreef <bdesth.quelquechose at free.quelquepart.fr>:
>>>
>>>>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.
>>
>> 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