How to unpack variables from tuple and give them names from another tuple?
<p>I have two tuples - one with the keys and another one with a collection of variables of different types (list, float64, int and array) generate with the help of the following formula from a dictionary:</p>

<pre><code>keys, values = zip(*[(key, value) for (key, value) in data_dict.items()])
</code></pre>

<p>Now I would like to retrieve the variables from the list of values and give them names from the list of keys. How can I do that?</p>

<p>If it's really not possible to store your key/value pairs in a <code>dict</code> and serialise them to data file in that format, you can use <a href="https://docs.python.org/3/library/functions.html#exec" rel="nofollow"><code>exec</code></a> to dynamically construct assignment statements</p>

<pre><code>&gt;&gt;&gt; key = 'foo_var'
&gt;&gt;&gt; val = 'foo_val'
&gt;&gt;&gt; exec('{key} = \'{val}\''.format(key=key, val=val))
&gt;&gt;&gt; foo_var
'foo_val'
</code></pre>

<p>I'd explore the <code>dict</code> approach first though, in case that can work for you. There are lots of good reasons to avoid runtime code generation using <code>exec</code> and <code>eval</code> where possible.</p>

python