salaries = {} salaries[('John', 'Smith')] = 10000.0 salaries[('John', 'Parker')] = 99999.0
EDIT 1 Of course, you can do salaries['John Smith'] = whatever , but then you will have to do additional work to separate the key from the first and last name. What about pointColor[(x, y, z)] = "red" , here the advantage of the tuple key is more noticeable.
I must emphasize that this is not the best practice. In many cases, you'd better create special classes to handle such situations, but Arrieta asked for examples that I gave her (her).
EDIT 0
By the way, each element of the tuple must also be hashed:
>>> d = {} >>> t = (range(3), range(10, 13)) >>> d[t] = 11 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: list objects are unhashable >>>
Boris Gorelik Dec 21 '09 at 7:06 2009-12-21 07:06
source share