'Is there a way to make each string from a list of strings into their own empty lists?
here's what im trying to do:
oglist = ['a','b','c']
#add magical code here and print empty created lists
a = []
b = []
c = []
Solution 1:[1]
Although, I strongly discouraged you to do it, you can create some variables dynamically with globals() or locals() (or vars()):
oglist = ['a', 'b', 'c']
for og in oglist:
globals()[og] = []
Test:
>>> a
[]
>>> b
[]
>>> c
[]
Recommended approach: It's preferable to use something like defaultdict:
from collections import defaultdict
ogdict = defaultdict(list)
Test:
>>> ogdict['a']
[]
>>> ogdict['b']
[]
>>> ogdict['c']
[]
As you can see with defaultdict, you don't need to have the key before in your dict. The entry is created dynamically.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Corralien |
