Another simple python example which is how to find out if a key exists in python
Just use "if" and "in" like in the following example
x['item1'] = 0 if 'item1' in x: print "True"
The above will print true
Some of the mistakes that I have seen is this will "appear" to work correctly but in fact it does not. Have a look at these examples. You can see here that it will fail because it is testing the value rather than the key.
x['item1'] = 1 if x['item1']: print "True"
The above will also print true
x['item1'] = 0 if x['item1']: print "True"
The above won't print true
Did You find this page useful?
Thanks for the feeback. Please consider sharing with others.