Most pythonic way of checking if a Key is in a dictionary and Value is not None
<p>I am using <code>flask</code> and <code>flask_restful</code> and has something like</p>

<pre><code>self.reqparse = reqparse.RequestParser()
self.reqparse.add_argument('OptionalArg', type=str, default=None)
self.reqparse.add_argument('OptionalArg2', type=str, default=None)
self.__args = self.reqparse.parse_args()

if 'OptionalArg' in self.__args and self.__args['OptionalArg'] is not None:
     # Do something with the OptionalArg only
     pass
else:
     # Do something with all the arguments that are not None.
     pass
</code></pre>

<p><a href="http://stackoverflow.com/a/7771511/1809168">Most relevant answer I found.</a></p>

<p>Though the question was asked a couple of years back, I was wondering if there is a much more pythonic way of checking if a <code>Key</code> is in a dictionary and <code>Value</code> is not <code>None</code>.</p>

<p>The reason I mentioned <code>flask</code> and <code>flask_restful</code> is to justify the initialization of <code>Keys</code> with <code>None</code> values within my dictionary.</p>

<p>Just use <code>dict.get</code> method with optional <code>default</code> parameter. It returns <code>d[key]</code> if <code>key</code> exists in the dictionary <code>d</code> and <code>default</code> value otherwise:</p>

<pre><code>In [1]: d = {1: 'A', 2: None}

In [2]: d.get(1)
Out[2]: 'A'

In [3]: d.get(2)

In [4]: d.get(1) is not None
Out[4]: True

In [5]: d.get(2) is not None
Out[5]: False

In [6]: d.get(3) is not None
Out[6]: False
</code></pre>

<p>For your case:</p>

<pre><code>if self.__args.get('OptionalArg') is not None:
     # Do something with the OptionalArg only
     pass
else:
     # Do something with all the arguments that are not None.
     pass
</code></pre>

python dictionary