I wonder how I can make lists of every string entry of a list? Let's say I have a list:
list_1 = ['a', 'b', 'c']
and I want to get automatically a list of every entry of the list above:
a = []
b = []
c = []
Is there a way to do that?
I wonder how I can make lists of every string entry of a list? Let's say I have a list:
list_1 = ['a', 'b', 'c']
and I want to get automatically a list of every entry of the list above:
a = []
b = []
c = []
Is there a way to do that?
You can use a dictionary to achieve something similar. Just use the names of your strings as dictionary keys and value as a list. `
list_1 = ['a', 'b', 'c']
dict1 = {}
for i in range(len(list_1)):
dict1[list_1[i]] = [10,20]
print(dict1)
which gives you
{'a': [10, 20], 'c': [10, 20], 'b': [10, 20]}
You may do that using a dictionary.
vars = {}
list_1 = ['a', 'b', 'c']
for i in list_1:
if i not in vars:
vars[i] = []
So your vars
dict will be
vars = {
'a': [],
'b': [],
'c': []
}
And then you can access to data like a normal variable:
>>> vars['a'].append(10)
>>> vars
{'a': [10], 'b': [], 'c': []}
>>> vars['a']
[10]