title: Creating lists in python that are dependent on previous elements
body: <p>I am learning python and am working on list comprehension right now.  I am doing a problem that asks to generate a list of lists of n elements where the first element in each inner list represents its index, the next n-2 elements are random numbers between 1 and 10, and the last element is the sum of the previous n-2 elements where the value was over 5. For example a sample would be </p>
  
  <pre><code>[[0, 5, 10, 3, 10], [1, 7, 3, 7, 14], [2, 2, 9, 5, 9]]
  </code></pre>
  
  <p>The code I used to generate the output above was</p>
  
  <pre><code>import random
  SUM = 0
  def genRandNum():
      global SUM
      x = random.randint(1,10)
      if(x &gt; 3):
          SUM = SUM + x
      return x
  
  def finalEle():
      global SUM
      ret = SUM
      SUM = 0
      return ret
  
  INNER_LIST_SIZE = 5
  OUTER_LIST_SIZE = 3
  li = [[y if x == 0
           else finalEle()if x == INNER_LIST_SIZE - 1
           else genRandNum()
           for x in range(INNER_LIST_SIZE)] for y in range(OUTER_LIST_SIZE)]
  print(li)
  </code></pre>
  
  <p>However, coming from a language like Java I am inclined to write my code like this below script:</p>
  
  <pre><code>import random
  SUM = 0
  THRESHOLD = 5
  INNER_LIST_SIZE = 5
  OUTER_LIST_SIZE = 3
  li = []
  for y in range(OUTER_LIST_SIZE):
      innerLi = []
      for x in range(INNER_LIST_SIZE):
          if x == 0:
              innerLi.append(y)
          elif x == INNER_LIST_SIZE-1:
              innerLi.append(SUM)
          else:
              rand = random.randint(1,10)
              innerLi.append(rand)
              if rand &gt; THRESHOLD:
                  SUM += rand
      li.append(innerLi)
      SUM = 0
  print(li)
  </code></pre>
  
  <p>Which one of these solutions looks like a better way to solve the problem from a python perspective? Are either of them actually how I should be doing this problem, and if not what are the other python tools that I could use to do this?</p>