Python: Can one create new variable names from a list of strings?
list, python, variables
Solution
Why don't you just construct a dictionary using the strings as keys?
>>> class test():
def handy(self):
a = raw_input('How many hands? ')
d = { "hand" + str(i + 1) : self.do_something(i) for i in range(int(a)) }
keys = d.keys()
keys.sort()
for x in keys:
print x, '=', d[x]
def do_something(self, i):
return "something " + str(i)
>>> test().handy()
How many hands? 4
hand1 = something 0
hand2 = something 1
hand3 = something 2
hand4 = something 3
Edit: You updated the question to ask if you can store a dictionary as a value in a dictionary. Yes, you can:
>>> d = { i : { j : str(i) + str(j) for j in range(5) } for i in range(5) }
>>> d[1][2]
'12'
>>> d[4][1]
'41'
>>> d[2]
{0: '20', 1: '21', 2: '22', 3: '23', 4: '24'}
>> d[5] = { 1 : '51' }
>> d[5][1]
'51'
Problem
I'm new to python, so I don't know if this is possible. I'm trying to create a list of variable names from a list of strings so I can then assign values to the newly created variables. I saw another similar question, but there was already a dict pays of keys and values. Here's my code: ``` def handy(self): a = raw_input('How many hands? ') for i in range(int(a)): h = "hand" + str(i+ 1) self.list_of_hands.append(h) # would like to create variable names from self.list_of_hands # something like "hand1 = self.do_something(i)" print h print self.list_of_hands ``` Given some of the answers, i'm adding the following comment: I'm going to then assign dicts to each variable. So, can I create a dictionary of dictionaries?