title: How to calculate moving average in Python 3?
body: <p>Let's say I have a list:</p>
  
  <pre><code>y = ['1', '2', '3', '4','5','6','7','8','9','10']
  </code></pre>
  
  <p>I want to create a function that calculates the moving n-day average.
  So if <code>n</code> was 5, I would want my code to calculate the first 1-5, add it and find the average, which would be 3.0, then go on to 2-6, calculate the average, which would be 4.0, then 3-7, 4-8, 5-9, 6-10. </p>
  
  <p>I don't want to calculate the first n-1 days, so starting from the nth day, it'll count the previous days.</p>
  
  <pre><code>def moving_average(x:'list of prices', n):
      for num in range(len(x)+1):
          print(x[num-n:num])
  </code></pre>
  
  <p>This seems to print out what I want:</p>
  
  <pre><code>[]
  []
  []
  []
  []
  
  ['1', '2', '3', '4', '5']
  
  ['2', '3', '4', '5', '6']
  
  ['3', '4', '5', '6', '7']
  
  ['4', '5', '6', '7', '8']
  
  ['5', '6', '7', '8', '9']
  
  ['6', '7', '8', '9', '10']
  </code></pre>
  
  <p>However, I don't know how to calculate the numbers inside those lists.  Any ideas?</p>