Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Creating Unique Dictionary Variables from List

Reply
Thread Tools

Creating Unique Dictionary Variables from List

 
 
Greg Corradini
Guest
Posts: n/a
 
      04-11-2007

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


--
View this message in context: http://www.nabble.com/Creating-Uniqu....html#a9943317
Sent from the Python - python-list mailing list archive at Nabble.com.

 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      04-11-2007
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()
 
Reply With Quote
 
 
 
 
Greg Corradini
Guest
Posts: n/a
 
      04-11-2007

Bruno,
Your help is much appreciated. I will give this a try tomorrow morning and
get back on how it works.


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()
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


--
View this message in context: http://www.nabble.com/Creating-Uniqu....html#a9947284
Sent from the Python - python-list mailing list archive at Nabble.com.

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      04-11-2007
Greg Corradini a écrit :
> Bruno,
> Your help is much appreciated.


Then give thanks to Dennis too !-)

> I will give this a try tomorrow morning and
> get back on how it works.


Don't worry, it just works - and it's the idiomatic solution to the
problem you described.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-11-2007
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)?

returnedlist = [x,y,z]
dicts = {}
for num, item in enumerate(returnedlist):
# presumably you would use item somewhere
dicts[num] = {item: None}

And that suggests that storing the dicts in a dict may be unnecessary --
just put them in a list:

returnedlist = [x,y,z]
dicts = [None] * len(returnedlist)
for num, item in enumerate(returnedlist):
dicts[num] = {item: None}



--
Steven.

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      04-12-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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a unique method in python to unique a list? Token Type Python 9 09-09-2012 02:13 PM
list question... unique values in all possible unique spots ToshiBoy Python 6 08-12-2008 05:01 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
Creating Unique Dictionary Variables from List Greg Corradini Python 2 04-11-2007 06:05 PM
Creating Unique Dictionary Variables from List Greg Corradini Python 0 04-11-2007 04:53 PM



Advertisments