Creating Unique Dictionary Variables from List

G

Greg Corradini

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) and add a
unique ending such that
# my final dictionary name would be dic + count for each i

Any ideas about this?
Greg
 
D

Dennis Lee Bieber

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:
said:
Any ideas about this?

Besides not posting three copies of the request? <G>

The simplest scheme is to use a (master) dictionary.

mDict = {}

for c, d in enumerate(aList):
mDict["dic_%s" % c] = {}
#do something with the data in d?
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
D

Dennis Lee Bieber

Talking to myself?
The simplest scheme is to use a (master) dictionary.

mDict = {}

for c, d in enumerate(aList):
mDict["dic_%s" % c] = {}
#do something with the data in d?

Unmentioned is that, if the name is just some constant with the
ordinal position from the list... why bother? Which would you rather
see/code:

#using random to represent some means of arbitrary access
for i in random.randint(0, len(mDict)-1):
key = "This_is_the_dictionary_of_list_item_number_%s" % i
something = mDict[key]["something"]

or the simplicity of using a list to begin with:

theDicts = []

for c, d in enumerate(aList):
theDicts.append({})
#do something with the data in d, referencing theDicts[c]

for i in random.randint(0, len(theDicts)-1):
something=theDicts["something"]


Now... If the "name" of the "variable" were based upon, say, the
first subelement of each list entry... Then the mDict model makes sense:

for d in aList:
mDict[d[0]] = d[1:] #or whatever is needed to populate the sub
dictionary
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,250
Members
46,848
Latest member
Graciela Mitchell

Latest Threads

Top