Python: quick way to create a list of n lists

So, I was wondering what is the best way to create a list of empty lists:

[[],[],[]...] 

Due to the way Python works with in-memory lists, this does not work:

 [[]]*n 

This creates [[],[],...] , but each item is the same list:

 d = [[]]*n d[0].append(1) #[[1],[1],...] 

Something like list comprehension works:

 d = [[] for x in xrange(0,n)] 

But it uses Python VM for looping. Is there a way to use an implied loop (using it in C)?

 d = [] map(lambda n: d.append([]),xrange(0,10)) 

This is actually slower. :(

+81
python
Apr 01 '11 at 20:23
source share
5 answers

Probably only a way that is a little faster than

 d = [[] for x in xrange(n)] 

is an

 from itertools import repeat d = [[] for i in repeat(None, n)] 

It does not need to create a new int object at each iteration and on my machine about 5% faster.

Change Using NumPy, you can avoid the Python loop by using

 d = numpy.empty((n, 0)).tolist() 

but it is 2.5 times slower than list comprehension.

+86
Apr 01 2018-11-11T00:
source share

Implementations are actually implemented more efficiently than an explicit loop (see the output of dis , for example, functions ) and the map method must refer to the object of doubt at each iteration, which incurs significant overhead.

Regardless, [[] for _dummy in xrange(n)] is the right way to do this, and none of the tiny (if at all) speed differences between the various methods should matter. Unless, of course, you spend most of your time, but then you should work on your algorithms instead. How often do you create these lists?

+12
Apr 01 '11 at 20:32
source share

Here are two methods: one is sweet and simple (and conceptual), the other is more formal and can be expanded in various situations after reading a dataset.

Method 1: Conceptual

 X2=[] X1=[1,2,3] X2.append(X1) X3=[4,5,6] X2.append(X3) X2 thus has [[1,2,3],[4,5,6]] ie a list of lists. 

Method 2: Formal and Extensible

Another elegant way to store a list as a list of lists of different numbers is that it reads from a file. (There is a data set in this file) A train is a data set, for example, 50 rows and 20 columns. i.e. Train [0] gives me the 1st line of the csv file, train [1] gives me the 2nd row and so on. I am interested in splitting the dataset into 50 rows as one list, except for column 0, which is my explained variable here, so it needs to be removed from the orignal train dataset, and then increase the list after the list, i.e. List list, Here is the code that does this.

Note that I read from "1" in the inner loop, as I am only interested in explanatory variables. And I reinitialize X1 = [] in another loop, otherwise X2.append ([0: (len (train [0]) - 1)]) will rewrite X1 again and again, moreover, it is more memory efficient .

 X2=[] for j in range(0,len(train)): X1=[] for k in range(1,len(train[0])): txt2=train[j][k] X1.append(txt2) X2.append(X1[0:(len(train[0])-1)]) 
+10
Apr 2 '13 at
source share

To create a list and list of lists, use the syntax below

  x = [[] for i in range(10)] 

this will create the 1st list and initialize it by placing the number in [[number] and set the length of the list line length in the range (length)

  • Use the syntax below to create a list of lists.

    x = [[[0] for i in the range (3)] for i in the range (10)]

, this will initialize the list of lists with a size of 10 * 3 and with a value of 0

  • To access / manage an item

    x [1] [5] = value

+2
Aug 11 '17 at 14:56 on
source share

So, I made some speed comparisons to get the fastest way. The list of concepts is really very fast. The only way to get closer is to avoid using bytecode while building the list. My first attempt was the following method, which, apparently, would be faster in principle:

 l = [[]] for _ in range(n): l.extend(map(list,l)) 

(displays a list of length 2 ** n, of course) This construction is twice as slow as comprehending the list, according to timeit, for short and long (millions) lists.

My second attempt was to use starmap to call the list constructor for me. There is one construct that seems to launch the list constructor at maximum speed, but still slower, but only a tiny amount:

 from itertools import starmap l = list(starmap(list,[()]*(1<<n))) 

Interestingly, the runtime prompts that this is the final call to the list, which makes the starmap solution slow because its runtime is almost exactly equal to speed:

 l = list([] for _ in range(1<<n)) 

My third attempt came when I realized that list (()) also creates a list, so I tried a simple simple:

 l = list(map(list, [()]*(1<<n))) 

but it was slower than calling starmap.

Conclusion: for speed maniacs: Use a list comprehension. Only calls if you need to. Use the built-in functions.

+1
08 Sep '15 at 7:18
source share



All Articles