Reputation:
I want a line of code which will create an empty list whose name is determined by an incrementing variable.
Everywhere I've looked thus far has been unhelpful. They suggest instead something which makes a list of empty lists or using a dictionary. This is not what I want, I want a line (or several) of code which just creates an empty list (which is not part of a dictionary or a list of lists, a stand alone entity). I want these lists to be distinct, distinguished by a variable which increments by 1 every time a new empty list is created.
These empty lists can't be made in one batch at the very beginning as I intend to make and delete these lists throughout a recursive function. I need to make multiple lists as my intention is for this function to descend to a new level of recursion but return later to the previous list. These are acting as temporary working lists which calculate a value for each item of an original list and then copy the result into a final list once it is found.
str("list_" + i) = []
If i = 1, then I would like this line of code to create an empty list with the name "list_1" which I can proceed to interact with throughout my script.
Instead I'm met with "SyntaxError: can't assign to function call"
By all means let me know if I'm not making any sense at all and I will do my best to convey my request.
Upvotes: 0
Views: 3024
Reputation: 164673
globals
, use defaultdict
Using a collections.defaultdict
is both efficient and manageable. You don't need to add keys explicitly when appending to list values:
from collections import defaultdict
dd = defaultdict(list)
dd['list_0'].append('foo')
print(dd)
# defaultdict(<class 'list'>, {'list_0': ['foo']})
If you want to explicitly add keys, even with empty list values, you can do so by feeding an extra argument:
from collections import defaultdict
dd = defaultdict(list, **{k: [] for k in [f'list_{i}' for i in range(5)]})
print(dd)
# defaultdict(<class 'list'>, {'list_0': [], 'list_1': [], 'list_2': [],
# 'list_3': [], 'list_4': []})
Upvotes: 0
Reputation: 3265
This is bad practice but anyway:
for i in range(10): # for global vars
globals()["list_" + str(i)] = []
print(list_0) # []
Maybe what you're looking for is a dictionary:
items = dict()
for i in range(10): # for dictionary
items[str("list_" + i)] = []
print(items.list_0) # []
Upvotes: 0
Reputation: 14216
Highly advise against this but this achieves what you want depending on your use case:
globals()['list_' + str(i)] = []
for i in range(5):
globals()['list_' + str(i)] = []
list_1
[]
list_2
[]
etc...
Depending on your use case switch globals
for locals
and vice-versa.
A better idea would be defaultdict
from collections import defaultdict
my_lists = defaultdict(list)
Now you don't have to initialize any list until you use it and every key comes with it's value being a list
already. For example:
for i in range(5):
my_lists['list_' + str(i)].append('foo')
my_lists
defaultdict(list,
{'list_0': ['foo'],
'list_1': ['foo'],
'list_2': ['foo'],
'list_3': ['foo'],
'list_4': ['foo']})
Upvotes: 1