Creating Unique Dictionary Variables from List

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Apr 12 16:09:16 EDT 2007


Steven D'Aprano a écrit :
> On Wed, 11 Apr 2007 21:03:20 +0200, Bruno Desthuilliers wrote:
> 
> 
>>Greg Corradini a écrit :
>>
>>>Hello All,
>>>I'm attempting to create multiple dictionaries at once, each with unique
>>>variable names. The number of dictionaries i need to create depends on the
>>>length of a list, which was returned from a previous function.
>>>The pseudo code for this problem would be:
>>>
>>>returnedlist = [x,y,z]
>>>count = 0
>>>for i in returnedlist:
>>>       if count < len(returnedlist):
>>>       # then create a dictionary (beginning with variable dic) for each i
>>>with a unique name such that
>>>       # my unique name would be dic + count
>>>
>>>Any ideas about this?
>>
>>Yes : use a dict to store your dicts:
>>
>>returnedlist = [x,y,z]
>>dicts = dict()
>>for num, item in enumerate(returnedlist):
>>    dicts['dict%s' % num] = dict()
> 
> 
> Given that num is unique each time around the loop, what do you gain by
> using 'dictN' for the key instead of just N (=num)?

The OP wanted such names, that's all.



More information about the Python-list mailing list