Why does an empty list become NoneType on return?
<p>I'm working on some path finding algorithms, and the snippet below is supposed to make an array of nodes in the path from the goal to the start. It works fine when there is a path from the goal to the start. But when there is no path from start to goal, the while loop never runs, and result gets returned as <code>[]</code> (which is correct).</p>

<pre><code>def path(goal, pathToParentLookup):
    currentNode = goal
    result = []
    while(currentNode in pathToParentLookup):
        currentNode = pathToParentLookup[currentNode]
        result.append(currentNode)

    return result

#bidirectional search from start to goal finds the mid point of "center"
start_path = path(center, pathBack_start).reverse()
goal_path = path(center, pathBack_goal)
return start_path + [center] + goal_path
</code></pre>

<p>However I'm getting this error:</p>

<pre class="lang-none prettyprint-override"><code>&lt;ipython-input-14-ca3cb26b31ce&gt; in bidirectional_search(graph, start, goal, searchMethod)
     46             start_path = path(center, pathBack_start).reverse()
     47             goal_path = path(center, pathBack_goal)
---&gt; 48             return start_path + [center] + goal_path
     49
     50

TypeError: can only concatenate list (not "NoneType") to list
</code></pre>

<p>That's not what is happening.  The problem is that on <code>line 46</code> you assign start_path the result of calling reverse() on the list that <code>path()</code> returns.
That's OK, but since <code>[].reverse()</code> always returns <code>None</code>, I'm certain it's not what you intended.</p>

<p>What I think you want is this:</p>

<pre><code>#bidirectional search from start to goal finds the mid point of "center"
start_path = path(center, pathBack_start)
start_path.reverse() 
goal_path = path(center, pathBack_goal)
return start_path + [center] + goal_path
</code></pre>

python list python-2.7 return nonetype