title: Python coding style - multiple return statements
body: <p>For the same task, I have coded two different functions. I would like to know which one is more elegant to use.</p>
  
  <p>The task is to check a <code>pydot</code> object if it beholds a requested node, and if so, to return the node and the graph object back. 
  If the node doesn't exist, the node needs to be created. </p>
  
  <p>To get the names of the nodes, I use the <code>pydot</code> objects <code>get_nodes()</code> function. However, if there aren't any nodes introduced yet, this function returns an empty list. Hence, before iterating over the values, I make an empty list check.</p>
  
  <p>The first variant (’variant1') is easy to understand. After the length check, which is necessary because of <code>node.get_name()</code>, it loops over to node names and once the node that is being searched for is found, the node and the graph is returned. If not, it calls a function that creates the node and updates the graph. Although this function is easy to understand, IMHO it is not elegant. It beholds two ’return’ statements:</p>
  
  <pre><code>def variant1(node_name, graph):
  
      if len(graph.get_nodes()) &gt; 0:
  
          for node in graph.get_nodes():
              if node_name == node.get_name():
                  return node, graph
  
      return create_node(node_name, graph)
  </code></pre>
  
  <p>The second variant, is quite more complicated. Once the node is found in the graph, it breaks and jumps directly to the last row (’return node, graph’). This variant has only one return statement.</p>
  
  <pre><code>def variant2(node_name, graph):
  
      if len(graph.get_nodes()) &gt; 0:
  
          for node in graph.get_nodes():
              if node_name == node.get_name():
                  break
  
          else:
              # the node doesnt exist. create it and update the graph
              node, graph = create_node(node_name, graph)
  
      else:
          # create the first node in the graph
          node, graph = create_node(node_name, graph)
  
      return node, graph
  </code></pre>
  
  <p>My question is, which one should I prefer to use, according to the ’The Zen of Python’, ’PEP8’ or ’Google Python Style Guide’?</p>