What is the difference between these two python methods?
<p>I am a python newbie. </p>

<p>I have one python method which returns the list recursively (previous is the dictionary of string and s is just a string that is included in the previous dictionary)</p>

<pre><code>def path(previous, s):
    "Return a list of states that lead to state s, according to the previous dict."
     return [] if (s is None) else path(previous, previous[s]) + [s]
</code></pre>

<p>and this one which I believe should return the same result</p>

<pre><code>def path(previous, s):
    "Return a list of states that lead to state s, according to the previous dict."
    if s is None:
        return []
    else:
        path(previous, previous[s]) + [s]
</code></pre>

<p>I was expecting that functionality wise those two methods are exactly identical, it's just that the first one is more consice. However, when I run the second methods, </p>

<p>I am receiving following error:  </p>

<blockquote>
  <p>"TypeError: unsupported operand type(s) for +: 'NoneType' and 'list'"</p>
</blockquote>

<p>What am I doing wrong here?</p>

<p>You're missing a <code>return</code> statement in the <em>else</em> branch of the second method:</p>

<pre><code>def path(previous, s):
    "Return a list of states that lead to state s, according to the previous dict."
    if s is None:
        return []
    else:
        return path(previous, previous[s]) + [s]
</code></pre>

<p>The first approach uses a <em>ternary operator</em> whose returned value (one of two) is <em>returned</em> by the <code>return</code> statement, therefore, the second needs a <code>return</code> statement in both branches.</p>

python breadth-first-search