-2

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)
  • Are you trying to create a Matrix (a List of Lists) or join all of the lists together into large list? – DogEatDog Jul 19 '21 at 21:19
  • 5
    If this was a code review, I would make you go back and replace those 100 individual lists with a single list `all_lists` that contains the lists. Then you just need `list_combined = list(itertools.chain.from_iterable(all_lists))`. – chepner Jul 19 '21 at 21:20
  • 2
    Your problem lies elsewhere. You should _not_ have 100 similarly called lists. You should have had a list of lists. – DYZ Jul 19 '21 at 21:20
  • 2
    "Suppose we have have 100 lists called list_1, list_2, ..., list_100" **that's your problem**. This should never occur. Instead, you should have used a *container*, like another list or a dict, to hold those lists. Don't try to dynamically use variables. Just re-design your entire approach – juanpa.arrivillaga Jul 19 '21 at 21:28

3 Answers3

2

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.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Couldn't you do this ` list_combined = [] for i in range(52): list_combined += 'list_{i}'.format(i) ` – timeinception23 Jul 19 '21 at 21:31
  • 1
    @timeinception23 no, you couldn't. This is **very important** to understand. `list_combined` is a `list` object, `'list_{i}'.format(i)'` **is a `str` object**. Variables **are not strings** and you shouldn't try to treat them as such. Again, you need to fundamentally change the way you are thinking about things. Variables are for source code, they are there for the *programmer*. As a programmer, you should be deciding what *data structures* your code requires. In this case, you should pretty obviously be using a `list` – juanpa.arrivillaga Jul 19 '21 at 21:34
0

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]
DogEatDog
  • 2,899
  • 2
  • 36
  • 65
-1

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.

StardustGogeta
  • 3,331
  • 2
  • 18
  • 32
  • 1
    A must read for everyone who uses `eval`: [Eval really is dangerous](https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html) – Matthias Jul 19 '21 at 21:33
  • You really, *really* shouldn't encourage this. – juanpa.arrivillaga Jul 19 '21 at 21:35
  • One could use `data += globals()[f'list_{i}']` (or something similar according to the scope) to avoid `eval`. Technically this answers the question. But the real answer is: Throw away those single variables for the lists and keep them in an appropriate data structure. – Matthias Jul 19 '21 at 21:45