title: Using "for x not in y"
body: <p>I'm trying to make a dynamic function that executes the Fibonacci sequence. It starts with a hashtable that assigns values to positions and then returns it. But if the position isn't in the hashtable, the code stores the value in the hashtable along with that position. Here's the code:</p>
  
  <pre><code> def dyn_fib(n):
       memo = {1:0, 2:1}
       if n &lt;= 2:
           return memo[n]
       else:
           for n not in memo:
               memo[n] = memo[n-2] + memo[n-1]
       return memo[n]
  </code></pre>
  
  <p>All I have to do is edit the 'else' statement to store new values in the hashtable using </p>
  
  <pre><code>memo[n] = memo[n-2] + memo[n-1] 
  </code></pre>
  
  <p>for any position "n" in not yet listed in memo, and then loop that until you get to "n". I thought I could use</p>
  
  <pre><code> for n not in memo:
  </code></pre>
  
  <p>but Geany is saying there is a syntax error. I think it's because if n = 5, it needs to compute 3 and 4 before 5 but I'm not sure how to put "for any position n not yet in memo" into a python statement. Anyone know how to do this? (I feel like it's simple but I'm not well versed in Python by any stretch.)</p>