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?

5
  • 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 Commented Oct 8, 2013 at 13:35
  • 4
    my_variable=[[] for _ in xrange(a)] Commented Oct 8, 2013 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]). Commented Oct 8, 2013 at 13:53
  • Something you may want instead: defaultdict(list). Commented Oct 8, 2013 at 14:37
  • This is a duplicate Commented Aug 22, 2017 at 17:06

2 Answers 2

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)]
Sign up to request clarification or add additional context in comments.

2 Comments

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?
@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: stackoverflow.com/questions/135041/…
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.

4 Comments

It would be helpful to demonstrate how this differs from [[]]*10
ya that is good.i missed it.
Consider expanding your answer to explain to the asker why this achieves the desired result, possibly linking to documentation. As is, this is only marginally useful.
@inspectorG4dget it differs like so: stackoverflow.com/questions/12791501/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.