[Tutor] variables into a dictionary

Magnus Lycka magnus@thinkware.se
Thu, 10 Oct 2002 22:24:37 +0200


At 08:55 2002-10-10 +0700, nano wrote:
>hello pythoners,
>i have some variables here:
>one =3D 1
>three =3D 4
>six =3D 6
>
>how can i assign those vars into a dictionary
>numbers=3D{'one':one,'three':three,'six':six}

You already have! :)

print locals()

locals contain more though. If you assign them all
in a row, I guess you could copy locals() before and
after, and use the difference.

before =3D locals().copy()
one =3D 2
three =3D 4
...
after =3D locals().copy()

myVars =3D {}
before_items =3D before.items()
for item in after.items():
     # Besides one, three, etc, the 'before' dict will be in 'after'
     if item[0] =3D 'before': continue
     if item not in before_items():
         myVars[item[0]] =3D item[1]


--=20
Magnus Lyck=E5, Thinkware AB
=C4lvans v=E4g 99, SE-907 50 UME=C5
tel: 070-582 80 65, fax: 070-612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se