Saturday, 15 February 2014

python - Check if key is in the dictionary? -



python - Check if key is in the dictionary? -

this question has reply here:

check if given key exists in dictionary 12 answers

how check if there key in dictionary?

here dictionary:

edges = {(1, 'a') : 2, (2, 'a') : 2, (2, '1') : 3, (3, '1') : 3}

i have tried this:

if edges[(1, 'a')]

but error:

traceback (most recent phone call last): file "vm_main.py", line 33, in <module> import main file "/tmp/vmuser_lvzelvvmfq/main.py", line 30, in <module> print fsmsim("aaa111",1,edges,accepting) file "/tmp/vmuser_lvzelvvmfq/main.py", line 16, in fsmsim if edges[(1, 'a')]: typeerror: 'dict' object not callable

what right way it?

just utilize in operator (which accompanied not in operator):

if (1, 'a') in edges: if (1, 'a') not in edges:

below excerpt documentation of python's dictionary type, d dictionary:

key in d

return true if d has key key, else false.

key not in d

equivalent not key in d.

python python-2.7 dictionary

No comments:

Post a Comment