1

I am trying to do the following in a concise way:

n = 3
lst1 = [[] for _ in range(n)]
lst2 = [[] for _ in range(n)]
lst3 = [[] for _ in range(n)]

Basically generating a number of list of empty lists with the same size.

I am looking for a one-liner on this one. I tried using zip and various itertools but I must admit this set of features is one of my weak links with Python.

dabadaba
  • 9,064
  • 21
  • 85
  • 155

3 Answers3

3

You can achieve the same with just one additional layer of list comprehension or generator expression and then unpacking it to variables:

>>> n = 3
>>> lst1, lst2, lst3 = ([[] for _ in range(n)] for _ in range(3))
>>> lst1
[[], [], []]
niemmi
  • 17,113
  • 7
  • 35
  • 42
  • I don't think the OP has mentioned how many such lists are required? Does it really depend on `n`? – AKS Dec 12 '16 at 12:21
  • Ah I see what I missed now, I need to wrap my head around these generators, iters and such... But I think the last `range(n)` is wrong. It should be as many as the number of vars to unpack there are, so in this case `3`. – dabadaba Dec 12 '16 at 12:23
  • @AKS: He hasn't but in the question he has created 3 of them. Doesn't really depend on `n` or hasn't been stated, changed example accordingly. – niemmi Dec 12 '16 at 12:24
  • @dabadaba: That is exactly what I was asking in the comments above. Exactly how many lists do you need? It pretty sure doesn't depend on `n` like you said. – AKS Dec 12 '16 at 12:25
  • @dabadaba: My bad, thought `n` had something to do with list size and the number of them, changed the answer accordingly. – niemmi Dec 12 '16 at 12:25
3

Instead of storing the lists in different variables, I will suggest to wrap all the list within single list. Your code should be like:

>>> num_of_list, size_of_list = 5, 3
>>> my_list = [[[] for _ in range(size_of_list)] for _ in range(num_of_list)]

The value hold by my_list will be:

[
   [[], [], []], 
   [[], [], []], 
   [[], [], []], 
   [[], [], []], 
   [[], [], []]
] 

For accessing each sub-list, you may simply use the index as:

>>> my_list[0]
[[], [], []]

>>> my_list[1]
[[], [], []]

If it is MUST to store the values in separate variables, you may unwrap the content of main list to individual variables as:

list1, list2, list3, list4, list5 = my_list
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • Not really. Even though here I named them `lst1`, `lst2`, etc... in practice they're going to be entirely separate (conceptually) ones, so it would not be a good idea to throw them in the same structure. – dabadaba Dec 12 '16 at 12:28
  • Updated the answer. I will suggest to initialize them as one variable say `raw_list` and then unwrap the values to variables for better clarity. My this comment is opinion based. – Moinuddin Quadri Dec 12 '16 at 12:30
  • Why would I want a temporary useless variable when I what those concise variables I know I am going to use? – dabadaba Dec 12 '16 at 12:32
  • Why is the variable useless? It acts as a wrapper object to all the lists you initialized together at the first place. By not declaring this variable are you saving your memory space? The answer is NO. Remaining sub-list variables will still point to existing objects. This is the pattern I follow and have seen advantages of it with time. As I mentioned, I **suggest** that. To follow or not is your wish :) – Moinuddin Quadri Dec 12 '16 at 12:37
  • Relax, I am just trying to understand your point of view, I think you took it way too bad. And well, sorry but I still don't see your point. And I never implied that would save memory, what I meant is that it seems a meaningless variable. Say that you want to have lists of `teams`, `scores` and `dates`. That's what you want. Not a `raw_list` with 3 sublists. What are you even going to do with `raw_list`? In this case, it serves no purpose, so why not just do what the other answer suggested? – dabadaba Dec 12 '16 at 12:43
-1
n = 3
for i in range(1, n+1):
    exec("lst%s = [[] for _ in range(%s)]" % (i, n))

Based on To convert string to variable name

Community
  • 1
  • 1
JCA
  • 167
  • 4