title: How can I avoid calling same function inside an inner pythonical style loop
body: <p>I have a high cpu consuming function, lets say:</p>
  <pre><code>def high_cost_function(p):
    ... lots of operations...
    return p
  </code></pre>
  <p>which I want to use under an inner loop in python:</p>
  <p>Example:</p>
  <pre><code>paths1= ['a','b','c']
  paths2= ['b','c','d']
  [any(x.startswith(high_cost_function(y)) for x in paths1) for y in paths2]
  </code></pre>
  <p>Which returns properly: <code>[True, True, False]</code></p>
  <p>However, as you can see, I am forced to call <code>high_cost_function(y)</code> for each of the x in paths1.</p>
  <p>If it was a normal loop, I would probably simply do:</p>
  <pre><code>for y in paths2:
    tmp_var = high_cost_function(y)
    for x in paths1:
      ...
      use tmp_var
      ...
  </code></pre>
  <p>Is there anyway to implement this in the first &quot;pythonical stylish way&quot;??</p>