Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The OP's solution is nearly there, there are just a few things to make it more Pythonic.</p> <p>For one, there's no need to get the length of the list. Empty lists in Python evaluate to False in an if check. Just simply say</p> <pre><code>if list: </code></pre> <p>Additionally, it's a very Bad Idea to assign to variables that overlap with reserved words. "list" is a reserved word in Python.</p> <p>So let's change that to</p> <pre><code>some_list = get_list() if some_list: </code></pre> <p>A really important point that a lot of solutions here miss is that <strong>all Python functions/methods return None by default</strong>. Try the following below.</p> <pre><code>def does_nothing(): pass foo = does_nothing() print foo </code></pre> <p>Unless you need to return None to terminate a function early, it's unnecessary to explicitly return None. Quite succinctly, just return the first entry, should it exist.</p> <pre><code>some_list = get_list() if some_list: return list[0] </code></pre> <p>And finally, perhaps this was implied, but just to be explicit (because <a href="http://www.python.org/dev/peps/pep-0020/" rel="noreferrer">explicit is better than implicit</a>), you should not have your function get the list from another function; just pass it in as a parameter. So, the final result would be</p> <pre><code>def get_first_item(some_list): if some_list: return list[0] my_list = get_list() first_item = get_first_item(my_list) </code></pre> <p>As I said, the OP was nearly there, and just a few touches give it the Python flavor you're looking for.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload