title: creating a dictionary with anonymous functions in Python
body: <p>I have a vector with some parameters and I would like to create a dictionary of anonymous (lambda) functions in Python3. </p>
  
  <p>The goal of this is to create a callable function that gives values equal to the sum of these functions with the same argument. </p>
  
  <p>I am struggling even to create a dictionary with the original lambda function objects and get them to behave consistently. I use the following code:</p>
  
  <pre><code>import numpy as np
  
  a = np.linspace(0,2.0,10)
  func = {}
  for i,val in enumerate(a):
    print(i)
    func['f{}'.format(i)] = lambda x: x**val
  
  print(func['f0'](0.5))
  print(func['f1'](0.5))
  print(func['f2'](0.5))
  print(func['f3'](0.5))
  </code></pre>
  
  <p>The output of the final print statements gives the same value, whereas I would like it to give the values corresponding to <code>x**val</code> with the value of <code>val</code> coming from the originally constructed array <code>a</code>. </p>
  
  <p>I guess what's happening is that the lambda functions always reference the "current" value of <code>val</code>, which, after the loop is executed is always the last value in the array? This makes sense because the output is:</p>
  
  <pre><code>0
  1
  2
  3
  4
  5
  6
  7
  8
  9
  0.25
  0.25
  0.25
  0.25
  </code></pre>
  
  <p>The output makes sense because it is the result of <code>0.5**2.0</code> and the exponent is the last value that <code>val</code> takes on in the loop. </p>
  
  <p>I don't really understand this because I would have thought <code>val</code> would go out of scope after the loop is run, but I'm assuming this is part of the "magic" of lambda functions in that they will keep variables that they need to compute the function in scope for longer. </p>
  
  <p>I guess what I need to do is to insert the "literal" value of <code>val</code> at that point into the lambda function, but I've never done that and don't know how.</p>
  
  <p>I would like to know how to properly insert the literal value of <code>val</code> into the lambda functions constructed at each iteration of the loop. I would also like to know if there is a better way to accomplish what I need to.  </p>
  
  <p>EDIT: it has been suggested that this question is a duplicate. I think it is a duplicate of <a href="https://stackoverflow.com/questions/45925683/list-comprehension-and-lambdas-in-python">the list comprehension post</a> because the best answer is virtually identical and lambda functions are used. </p>
  
  <p>I think it is not a duplicate of <a href="https://stackoverflow.com/questions/233673/how-do-lexical-closures-work">the lexical closures</a> post, although I think it is important that this post was mentioned. That post gives a deeper understanding of the underlying causes for this behavior but the original question specifically states "mindful avoidance of <code>lambda</code> functions," which makes it a bit different. I'm not sure what the purpose of that mindful avoidance is, but the post did teach related lessons on scoping. </p>