-1

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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Steve Iron
  • 241
  • 6
  • 12
  • Is there something you don't understand about the answers there? Use a dictionary, don't create "variable variables". Otherwise [edit] to clarify why you want to do this and what you still want to know. – jonrsharpe May 18 '17 at 08:30
  • Note now you've accepted an answer that was also on the duplicate! – jonrsharpe May 18 '17 at 09:30

2 Answers2

0

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]}
codelyzer
  • 467
  • 1
  • 4
  • 17
0

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]
chestacio
  • 26
  • 3