469,928 Members | 1,900 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,928 developers. It's quick & easy.

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 2578
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Fuzzyman | last post: by
2 posts views Thread by kevin parks | last post: by
4 posts views Thread by Livin | last post: by
1 post views Thread by Danny Liberty | last post: by
reply views Thread by Greg Corradini | last post: by
reply views Thread by Gabriel Genellina | last post: by
14 posts views Thread by tdahsu | last post: by
5 posts views Thread by =?Utf-8?B?UGF1bA==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.