473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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(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.

Apr 11 '07 #1
5 2835
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()
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:

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.

Apr 11 '07 #3
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.
Apr 11 '07 #4
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.

Apr 11 '07 #5
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.
Apr 12 '07 #6

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

Similar topics

23
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*...
2
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...
4
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...
1
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...
5
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...
0
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...
6
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...
0
by: Gabriel Genellina | last post by:
En Fri, 18 Apr 2008 12:23:08 -0300, Shawn Milochik <Shawn@Milochik.comescribió: A dictionary with keys is perfectly reasonable. But a *list* of values has to be searched linearly for every...
14
by: tdahsu | last post by:
I have twenty-five checkboxes I need to create (don't ask): self.checkbox1 = ... self.checkbox2 = ... .. .. .. self.checkbox25 = ... Right now, my code has 25 lines in it, one for each...
5
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a list of type object. The object has an ID as one of the elements and I would like to create another list that just has objects with unique IDs. For example in the list if I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.