How to eval() a string in a dictionary Python2.7
<p>I have a dictionary:</p>

<pre><code>key = {'K_z':'K_w'}
</code></pre>

<p>I want to get K_w as a value name instead of a string, I heard that the eval() function can convert strings to value names.</p>

<p>But when I type:</p>

<pre><code>print(eval(key['K_z']))
</code></pre>

<p>I get 'K_z' instead of having the K_w value.</p>

<p>Can you help me ?</p>

<p>If it helps, I'm using it for pygame to let the player change the key bindings, specially with AZERTY and QWERTY differences...</p>

<p>Is this what you are looking for?</p>

<pre><code>key = {'K_z':'K_w'}
print(key['K_z'])
</code></pre>

<p>K_w</p>

<p>I don't think that <code>eval()</code> will help as it evaluates arguments. In pygame you could use <code>if key[pygame.K_w] or key[pygame.K_z]</code> do something.</p>

python dictionary pygame eval