if statement - If conditions for Python dictionaries -
so have string converted dictionary called test , takes form:
{"a":"1","b":"2","c":"3"}
now while creating dictionary, availablilty of "c":"3"
key-pair, depends on condition; there cases when dictionary reads as:
{"a":"1","b":"2"}
i have if
condition checks existence of "c"
key , gives output based on it. condition follows:
if (test["c"]): print "hello world!!"
this has no else condition. problem arises when 3rd key-value pair isn't in dictionary. running code gives following error:
file "test.py", line 14, in test if test["c"]: keyerror: 'c'
may know can resolve , correct in code?
use in
check if key in dictionary:
if "c" in test: ...
Comments
Post a Comment