ranger_sim_g
ranger_sim_g

Reputation: 175

Need a faster and efficient way to add elements to a list in python

I am trying create a large list of numbers .
a = '1 1 1 2 2 0 0 1 1 1 1 9 9 0 0' (it goes over a ten million).

I've tried these methods:

  1. %timeit l = list(map(int, a.split())) it was 4.07 µs per loop
  2. %timeit l = a.split(' ') this was 462 ns per loop
  3. %timeit l = [i for i in a.split()] it took 1.19 µs per loop

I understand that the 2nd and 3rd variants are character lists whereas first is an integer list, this is fine. But as the number of elements gets to over ten million, it can take up to 6 seconds to create a list. This is too long for my purposes. Could someone tell me a more faster and efficient way to do this.

Thanks

Upvotes: 3

Views: 1784

Answers (3)

Adam Hughes
Adam Hughes

Reputation: 16309

I tested the various answers in a jupyter notebook, and Peter de Rivas does seem to edge out the others proposed.

enter image description here

Interestingly, the mapping to integer seems to be the bottleneck. The str.split() operation itself is an order of magnitude faster.

enter image description here

Upvotes: 2

Raymond Hettinger
Raymond Hettinger

Reputation: 226486

In plain Python, not using a third-party extension, a.split() ought to be the fastest way to split your input into a list. The str.split() function only has one job and it is specialized for this use.

Upvotes: 4

Peter de Rivaz
Peter de Rivaz

Reputation: 33509

If you know your input consists of single digits separated by single spaces then you can also consider:

b = ord('0')
[ord(a)-b for a in A[::2]]

this makes a list of 10 million integers in 0.2 seconds on my computer.

Upvotes: 3

Related Questions