Creating variables from dicts
Arnaud Delobelle
arnodel at googlemail.com
Tue Feb 23 16:10:14 EST 2010
vsoler <vicente.soler at gmail.com> writes:
> Hi,
>
> I have two dicts
>
> n={'a', 'm', 'p'}
> v={1,3,7}
These are sets, not dicts.
> and I'd like to have
>
> a=1
> m=3
> p=7
As sets are unordered, you may as well have
a = 3
m = 7
p = 1
or any other permutation. You need some sequences instead. E.g.
n = ['a', 'm', 'p']
v = (1, 3, 7)
Then you can do:
for name, value in zip(n, v):
globals()[name] = value
After this the names, 'a', 'm' and 'p' will be bound to the values you
want in the global namespace. However, it is almost always a bad idea
to do this. Can you explain why you need to do this?
--
Arnaud
More information about the Python-list
mailing list