473,890 Members | 1,732 Online

# Creating Unique Dictionary Variables from List

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(returnedlis t):
# then create a dictionary (beginning with variable dic) for each i
with a unique name such that
# my unique name would be dic + count

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.

Apr 11 '07 #1
5 2890
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(returnedlis t):
# then create a dictionary (beginning with variable dic) for each i
with a unique name such that
# my unique name would be dic + count

Yes : use a dict to store your dicts:

returnedlist = [x,y,z]
dicts = dict()
for num, item in enumerate(retur nedlist):
dicts['dict%s' % num] = dict()
Apr 11 '07 #2

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

>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(returnedlis t):
# then create a dictionary (beginning with variable dic) for each
i
with a unique name such that
# my unique name would be dic + count

Yes : use a dict to store your dicts:

returnedlist = [x,y,z]
dicts = dict()
for num, item in enumerate(retur nedlist):
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.

Apr 11 '07 #3
Bruno,
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.
Apr 11 '07 #4
On Wed, 11 Apr 2007 21:03:20 +0200, Bruno Desthuilliers wrote:
>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(returnedlis t):
# then create a dictionary (beginning with variable dic) for each i
with a unique name such that
# my unique name would be dic + count

Yes : use a dict to store your dicts:

returnedlist = [x,y,z]
dicts = dict()
for num, item in enumerate(retur nedlist):
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(retur nedlist):
# 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(returnedlis t)
for num, item in enumerate(retur nedlist):
dicts[num] = {item: None}

--
Steven.

Apr 11 '07 #5
Steven D'Aprano a écrit :
On Wed, 11 Apr 2007 21:03:20 +0200, Bruno Desthuilliers wrote:

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

returnedli st = [x,y,z]
count = 0
for i in returnedlist:
if count < len(returnedlis t):
# then create a dictionary (beginning with variable dic) for each i
with a unique name such that
# my unique name would be dic + count

Yes : use a dict to store your dicts:

returnedlis t = [x,y,z]
dicts = dict()
for num, item in enumerate(retur nedlist):
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.
Apr 12 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 23 40671 by: Fuzzyman | last post by: Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or* just created a new pointer to the original value.... For example I wanted to initialize a list of empty lists.... a=, , , , ] I thought there has to be a *really* easy way of doing it - after a 2 2313 by: kevin parks | last post by: hi. I've been banging my head against this one a while and have asked around, and i am throwing this one out there in the hopes that some one can shed some light on what has turned out to be a tough problem for me (though i am getting closer). i have been mucking with a lot of data in a dictionary that looks like: 4 2002 by: Livin | last post by: I need to dynamically create dictionary names using strings input at the time of creation. These will then be placed into a "Parent" dictionary. I'm new to python, and programming, so please bear with me. here's my initial thought but I don't think it will work... item='Kitchen Ceiling Lights' devDictName = item.replace(' ','+') 'dict_'+devDictName = , 'Status':item, 1 6376 by: Danny Liberty | last post by: I need some opionions on an issue here... Suppose I want to keep a collection of objects, each need to be uniquely identified by a number. This number has no meaning as long as it's unique, so it should be automatically generated for each new object. One more requirement is keeping the objects in the order in which they were inserted, not sorted by their ID. Considering the following options of implementation, which would be the best... 5 1479 by: blisspikle | last post by: I figure that someone good at dotnet can look at this and give me a clue on how to easily organize this code? If there is a unique identifier like "Publisher" with a bunch of "Book" that are published under them (I used the arraylist class in the publisher class). How should the code be organized, and how can the books properties like "Name" be easily called in the main code, or searched for in the main code? 0 1130 by: Greg Corradini | last post by: 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 = count = 0 for i in returnedlist: if count < len(returnedlist): 6 3802 by: =?Utf-8?B?bWFnZWxsYW4=?= | last post by: Hi, I'd appreciae any advice on how to do this in VB 2005. I have a simple array;e..g, a list of States, e.g., CA, WA, ID, AL, and etc... I like to determine how many unqiue "States" are in this array...ie., duplicate entires, are igorned... 0 1390 by: Gabriel Genellina | last post by: En Fri, 18 Apr 2008 12:23:08 -0300, Shawn Milochik