1

I want to write a python script that creates several dictionaries at once then print the dictionaries out but I don't know how to convert a string to a variable.

number = 0 while (number < 10): number = number + 1 dictionarynumber = ("D"+str(number)) # creates the dictionarys name(D1,D2,D3,D4,D5,D6,D7,D8,D9,D10) var(dictionarynumber) = {"Fruit":"Apple","Book":"Oliver Twist","Building":"White House"} #Of course var() doesn't work but I placed it here to get my point across print(dictionarynumber) 

After Answers:

I liked the idea about dictionaries and taking out the unneeded "D" makes sense. What do you guys think of this?

dict = {} n = 0 while (n < 10): n = n + 1 d = n d = {"Key":"Information"} dict[n] = d print(dict) # Output = # {1: {'Key': 'Information'}, # 2: {'Key': 'Information'}, # 3: {'Key': 'Information'}, # 4: {'Key': 'Information'}, # 5: {'Key': 'Information'}, # 6: {'Key': 'Information'}, # 7: {'Key': 'Information'}, # 8: {'Key': 'Information'}, # 9: {'Key': 'Information'}, # 10: {'Key': 'Information'}} 
4
  • 5
    Use a list (leave off the "D" in the indexer) or another dictionary. You'll thank yourself later. Commented Apr 15, 2012 at 4:58
  • Or use a dictionary-of-dictionaries -- a computed key is still better than a computer name -- if you want to be able to name them arbitrarily rather than just with sequential numbers. Commented Apr 15, 2012 at 5:00
  • 1
    Your number loop should instead be: for number in range(1, 11): Commented Apr 15, 2012 at 5:07
  • 2
    Yes, you can go crazy with dictionaries. ({1: {'a': {'i': 1, 'ii': 2), 'b': 3}, 2: 4, 3: {'knight': 'ni', 'parrot': None}} Just don't try to use a dictionary as a key. Commented Apr 15, 2012 at 5:11

2 Answers 2

4

If you want to give each dictionary a name, put them in a dictionary.

dicts = {} # dict_list = [] for i in xrange(10): dictionary_key = ('d{0}'.format(i)) dict_item = {"Fruit":"Apple","Book":"Oliver Twist","Building":"White House"} dicts[dictionary_key] = dict_item # dict_list.append(dict_item) 

If you're not hung up on using a name for your dictionaries, put them in a list.

Sign up to request clarification or add additional context in comments.

1 Comment

A blank dictionary, I didn't know you could do that. Seems like a good idea.
0

You seem to be trying to create a mixed data type but I'm unclear on what kinda structure you want so I'll give you a 2 answers and hope one is right.

1st, If you are trying to create a bunch of dictionaries and print it you would do it like this:

diclist = [{"Fruit":"Apple"},{"Book":"Oliver Twist"},{"Building":"White House"}] print(diclist[1]) 

2nd, going by the example you have given you might be aiming to create a dictionary of lists. For example

listDic = {'Fruit':['Apples', 'Bannanas', 'Durian'], 'Book':['Oliver Twist','Flashman', 'Catch 22']} print (listDic) 

and you can access it like this:

print(listDic['Fruit']) 

would result in

['Apples', 'Bannanas', 'Durian']

and this print(listDic['Fruit'][1]) would result in

Bannanas

If I missed the answer you wanted or you want more detail just drop a comment.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.