Reputation:
I have a list that came from an excel doc:
["GT", "FSU", "Duke"]
I am trying to make an empty list for each item in this list (to look like this):
GTList = []
FSUList = []
Duke = []
Is there an easy way to go from step 1 to step 2. I need to use loops too because there is a possibility of there being an extra school in the first list. If this is the case, there needs to be a corresponding empty list for that new school. Please let me know what advice you have.
Upvotes: 3
Views: 91
Reputation: 2033
I answered similar question earlier today. Try this:
a = ["GT", "FSU", "Duke"]
for i in a:
locals()[i] = []
print GT, FSU, Duke
Output:
[ ] [ ] [ ]
Demo.
Also, here's why you shoudn't be creating dynamic variables which is why I recommend Alexander's answer
Upvotes: 0
Reputation: 109510
I would use a dictionary comprehension:
schools = ["GT", "FSU", "Duke", "Stanford"]
schools_list = {school: [] for school in schools}
>>> schools_list
{'Duke': [], 'FSU': [], 'GT': [], 'Stanford': []}
schools_list['Stanford'].append('sucks')
>>> schools_list['Stanford']
['sucks']
Upvotes: 2