title: Get first element of list if list is not None: Python
body: <p>Here is my issue:</p>
  
  <p>I am doing an LDAP search in Python. The search will return a dictionary object:</p>
  
  <pre><code>{'mail':['user@mail.com'],'mobile':['07852242242'], 'telephoneNumber':['01112512152']}
  </code></pre>
  
  <p>As you can see, the returned dictionary contains list values.</p>
  
  <p>Sometimes, no result will be found:</p>
  
  <pre><code>{'mail':None, 'mobile':None, 'telephoneNumber':['01112512152']}
  </code></pre>
  
  <p>To extract the required values, I am using get() as to avoid exceptions if the dictionary item does not exist.</p>
  
  <pre><code>return {"uname":x.get('mail')[0], "telephone":x.get('telephoneNumber')[0], "mobile":x.get('mobile')[0]}
  </code></pre>
  
  <p>I want to return my own dictionary as above, with just the string values, but Im struggling to find an efficient way to check that the lists are not None and keep running into index errors or type errors:</p>
  
  <pre><code>(&lt;type 'exceptions.TypeError'&gt;, TypeError("'NoneType' object is unsubscriptable",)
  </code></pre>
  
  <p>Is there a way to use a get() method on a list, so that if the list is None it wont throw an exception???</p>
  
  <pre><code>{"uname":x.get('mail').get(0)}
  </code></pre>
  
  <p>What is the most efficient way of getting the first value of a list or returning None without using:</p>
  
  <pre><code>if isinstance(x.get('mail'),list):
  </code></pre>
  
  <p>or</p>
  
  <pre><code>if x.get('mail') is not None:
  </code></pre>