Sets in Python

Mark Dickinson dickinsm at gmail.com
Wed Sep 19 20:25:22 EDT 2007


On Sep 19, 7:26 pm, Karthik Gurusamy <kar1... at gmail.com> wrote:
> If I did, a = [10, 20] and I did d[a]= 'foo', then a.append(30).
> If dict complains key error on d[a] now, I won't be surprised. If I do
> d[[10, 20, 30]], I will be surprised if it doesn't find the item. Of
> course, in today's behavior the above is syntax error.

It sounds as though you're proposing something like the following:

>>> k = mylist([1, 2])
>>> d = {k : 'test'}
>>> d[k]
'test'
>>> k.append(3)
>>> d[k]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: [1, 2, 3]

So far, so good.  But how do you explain the following to a confused
newcomer?

>>> d.keys()
[[1, 2, 3]]
>>> k in d.keys()
True
>>> k in d
False
>>> d[k]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: [1, 2, 3]

In other words, to repeat Sion Arrowsmith's question, what would you
expect d.keys() to return after a key of d has been modified?

Mark




More information about the Python-list mailing list