Variable definition

John Posner jjposner at optimum.net
Fri Feb 26 20:15:16 EST 2010


On 2/26/2010 6:32 PM, Raphael Mayoraz wrote:
> Hello,
>
> I'd like to define variables with some specific name that has a common
> prefix.
> Something like this:
>
> varDic = {'red': 'a', 'green': 'b', 'blue': 'c'}
> for key, value in varDic.iteritems():
> 'myPrefix' + key = value
>

No trick, just swap a new key-value pair for each existing pair:

   for key, value in varDic.iteritems():
       varDic[myPrefix + key] = value
       del varDict[key]

Just make sure that *myPrefix* isn't an empty string!

-John



More information about the Python-list mailing list