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

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -