different key, same value in dictionaries

Magdoll magdoll at gmail.com
Sat Feb 9 22:51:34 CET 2008


Is there a cleaner way to do this example:

d = {('a','b'): 10, ('a','c'): 20, ('b','c'): 30}

The key is always a pair (x,y), but d[(x,y)] should have the same
result as d[(y,x)]. So either I would have to store both d[(x,y)] and
d[(y,x)] (unncessary extra space?), or write something like:

if x <= y: return d[(x,y)]
else:       return d[(y,x)]

I'm not familiar with python enough, so I want to know whether these
are my only choices....



More information about the Python-list mailing list