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

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Sep 27 19:32:45 CEST 2015


On 27/09/2015 17:38, bruce wrote:
> Hi.
>
> I can do a basic
>   a=[]
> to generate a simple list..
>
> i can do a a="aa"+bb"

Really?

 >>> a="aa"+bb"
   File "<stdin>", line 1
     a="aa"+bb"
              ^
SyntaxError: EOL while scanning string literal

No.  Moral of the story, never type something in directly, always cut 
and paste using the interactive interpreter.

>
> 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

I've no idea what you mean in this context.  `a` is the name you are 
assigning some object to.  `a = []` is a list, but having `a` be "aabb" 
means `a` is a string. So do you want:-

 >>> a=2*'a'+2*'b'
 >>> a
'aabb'

Or

 >>> a=[2*'a'+2*'b']
 >>> a
['aabb']

Or what?

>
> IRC replies have been "don't do it".. or it's bad.. but no one has
> said you can do it this way..

What replies?  Please be extremely cautious on sites that are nowhere 
near as precise as Python ones, for example stackoverflow or reddit. 
There is some complete dross out there that gets upvoted, whereas on a 
Python site it would be torn to pieces.  To be fair, some answers are 
good, please just be careful with what you read.

>
> just curious..
>
> thanks

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list