How to define a temporary variable in python?
<p>Does python have a "temporary" or "very local" variable facility?  I am looking for a one-liner and I want to keep my variable space tidy.</p>

<p>I would like to do something like this:</p>

<pre><code>...a, b, and c populated as lists earlier in code...
using ix=getindex(): print(a[ix],b[ix],c[ix])
...now ix is no longer defined...
</code></pre>

<p>The variable ix would be undefined outside of the one line.</p>

<p>Perhaps this pseudo-code is more clear:</p>

<pre><code>...a and b are populated lists earlier in code...
{ix=getindex(); answer = f(a[ix]) + g(b[ix])}
</code></pre>

<p>where ix does not exist outside of the bracket.</p>

<p>Technically not an answer, but: Don't worry about temporary variables too much, they are only valid within their local scope (most likely function in your case) and garbage collector gets rid of them as soon as that function is finished. One liners in python are mostly used for dict and list comprehensions. </p>

<p>If you REALLY want to do it in one line, use <code>lambda</code> which is pretty much keyword for inline function</p>

python