12

I want to have a variable that is a nested list of a number of empty lists that I can fill in later. Something that looks like:

my_variable=[[], [], [], []]

However, I do not know beforehand how many lists I will need, only at the creation step, therefore I need a variable a to determine it. I thought about simple my_variable=[[]]*a, but that creates copies of lists and it is not what I want to have.

I could do:

my_variable=[]  
for x in range(a):
   my_variable.append([])

but I'm looking for a more elegant solution (preferably one-liner). Is there any?

Andy S. C.
  • 368
  • 1
  • 4
  • 17
  • 3
    I think you need to rethink this, This seems like it could be better to use a dictionary for your needs. Consult [this post](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Inbar Rose Oct 08 '13 at 13:35
  • 4
    `my_variable=[[] for _ in xrange(a)]` – inspectorG4dget Oct 08 '13 at 13:36
  • +1 to Inbar Rose's comment... i had to convert some perl code (where you can instantiate any index at any time, which python doesn't allow.. x = [], x[4] = 1 works in perl, throws an error in python). however, just make x a dictionary, and it will work the same way: x = {}, x[4] = 1 now works just fine. you will have to sort the keys, and they won't be monotonic though (i.e. you could have indexes like [0,3,4,7]). – Corley Brigman Oct 08 '13 at 13:53
  • Something you may want instead: [defaultdict(list)](http://docs.python.org/3/library/collections.html?highlight=defaultdict#collections.defaultdict). – Mark Tolonen Oct 08 '13 at 14:37
  • This is a duplicate – dǝɥɔS ʇoıןןƎ Aug 22 '17 at 17:06

2 Answers2

18

Try a list comprehension:

lst = [[] for _ in xrange(a)]

See below:

>>> a = 3
>>> lst = [[] for _ in xrange(a)]
>>> lst
[[], [], []]
>>> a = 10
>>> lst = [[] for _ in xrange(a)]
>>> lst
[[], [], [], [], [], [], [], [], [], []]
>>> # This is to prove that each of the lists in lst is unique
>>> lst[0].append(1)
>>> lst
[[1], [], [], [], [], [], [], [], [], []]
>>>

Note however that the above is for Python 2.x. On Python 3.x., since xrange was removed, you will want this:

lst = [[] for _ in range(a)]
  • I mast have had a black-out, that I haven't thought about list comprehension before. :) However, I'm wondering why `xrange` on Python 2.x is better than `range`? They both work the same, aren't they? – Andy S. C. Oct 08 '13 at 13:52
  • 1
    @AndyS.c. - Not actually. In Python 2.x, `xrange` (which returns an xrange iterator) is faster than `range` (which returns a list). In Python 3.x. however, `range` was made to do what `xrange` does and so `xrange ` was removed. Here is a reference: http://stackoverflow.com/questions/135041/should-you-always-favor-xrange-over-range –  Oct 08 '13 at 13:54
14
>>>[[] for x in range(10)] #To make a list of n different lists, do this:
[[], [], [], [], [], [], [], [], [], []]

Edit :-

[[]]*10

This will give you the same result like above but the list are not distinct instances,they are just n references to the same instance.

Kousik
  • 21,485
  • 7
  • 36
  • 59