[Tutor] generate a list/dict with a dynamic name..

Chris Warrick kwpolska at gmail.com
Sun Sep 27 18:45:20 CEST 2015


On 27 September 2015 at 18:38, bruce <badouglas at gmail.com> wrote:
> Hi.
>
> I can do a basic
>  a=[]
> to generate a simple list..
>
> i can do a a="aa"+bb"
>
> how can i do a
>  a=[]
>
> where a would have the value of "aabb"
>
> in other words, generate a list/dict with a dynamically generated name
>
> IRC replies have been "don't do it".. or it's bad.. but no one has
> said you can do it this way..
>
> just curious..
>
> thanks
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

And we really mean it.

There are two ways.  One involves e**l, which means you are executing
arbitrary code, and if a contained some malicious code, it could break
your system.  Using e**l is considered bad for this precise reason:
you don’t know if the input might lead to formatting your hard drive.
And you should not trust it to be good.

The other way involves modifying the g*****s() dict.  It does not
always work though.

But for a REAL way to do it, just create a dict and use it — you can
have arbitrary variable names just fine:

things = {}
a = "aabb"
things[a] = []

PS. why are you creating a out of two strings?  Why not just a = "aabb"?

-- 
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16


More information about the Tutor mailing list