Suppose we have have 100 lists called list_1
, list_2
, ..
., list_100
If we want to combine these lists, how would we do this? Would something like this work:
for i in range(101):
list_combined += list_{i}
print(list_combined)
Suppose we have have 100 lists called list_1
, list_2
, ..
., list_100
If we want to combine these lists, how would we do this? Would something like this work:
for i in range(101):
list_combined += list_{i}
print(list_combined)
No, that's not how you do it. Instead you should create a dictionary (or list) called lists
, and then create lists[0]
, lists[1]
, lists[2]
, etc. With that, it's trivially easy to iterate through all the lists.
To combine a list of lists, you could do the following:
my_lists = [
[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6],
]
combined_lists = []
for my_list in my_lists:
combined_lists += my_list
print(combined_lists)
# which prints
>>> [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
If you absolutely must have 100 lists as specified in the question, you can do the following:
list_combined = []
for i in range(1, 101):
list_combined += eval(f"list_{i}")
print(list_combined)
I strongly discourage this approach in favor of making a list of lists instead, but it will work.