Variable definition

Raphael Mayoraz maygeo at netplus.ch
Mon Mar 1 13:07:17 EST 2010


John Posner wrote:
> 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
Thanks for your answer.
However, your solution changes the key name in the dictionary.
That's not what I want I need to do. What I want is to define a new
variable which name is define as a string: 'myPrefx' + key. In the example
I give, I would get 3 variables:
myPrefixred = a
myPrefixgreen = b
myPrefixblue = c

Raphael



More information about the Python-list mailing list