I have a dictionary having tuple as its key and also tuple as its values. I need a way to access the values of dictionary based on its keys. For example:
d = {}
d = { (1, 2) : ('A', 'B'),(3, 4) : ('C', 'B') }
Now, first I need to check if keys (1, 2) already exists in a dictionary.
Something like:
if d.has_key(1,2)
print d[1]
print d[2]
You can simply use a literal tuple as the key:
>>> d = {(1, 2): ('A', 'B'), (3, 4): ('C', 'D')}
>>> (1, 2) in d
True
>>> d[(1, 2)]
('A', 'B')
>>> d[(1, 2)][0]
'A'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With