title: For loop stops iterating in recursive function
body: <p>I'm attempting to solve the <a href="https://www.hackerrank.com/contests/codeagon/challenges/dependency-hell" rel="nofollow noreferrer">dependency hell</a> problem on Hackerrank using Python 2.7.</p>
  
  <p>I have formed a dictionary which lists all the dependencies. This is how the dictionary looks:</p>
  
  <pre><code>d = {1: [1, 8], 2: [0], 3: [1, 7], 4: [1, 7], 5: [1, 2], 6: [1, 2], 7: [0], 8: [2, 3, 4]}
  </code></pre>
  
  <p>The first number in each of the lists is just the number of elements in that list. The next numbers are the indices of the dependencies. Each number is the index for a software (i.e. software #1).</p>
  
  <p>I've tried to create a recursive function that given an index, would list all the SW that need to be installed for the intended SW to be installed. I'm not worrying about the order at this point. This is the function:</p>
  
  <pre><code>def listSW(p,d,L):
      L.append(p)
      if d[p][0] &gt; 0:
          for i in xrange(1,d[p][0]+1):
              return listSW(d[p][i],d,L)
      else:
          return L
  </code></pre>
  
  <p>Where <code>L</code> is a list (initially empty) where I'm dumping the indices of everything that needs to be installed and <code>p</code> is the software we intend to install.</p>
  
  <p>The problem is that in the <code>for</code> loop only <code>i = 1</code> is ever considered. So if I run the function for <code>p = 8</code> I get:</p>
  
  <pre><code>[8, 3, 7]
  </code></pre>
  
  <p>When I intend to get:</p>
  
  <pre><code>[8, 3, 7, 4, 7]
  </code></pre>
  
  <p>What am I doing wrong?</p>