Reputation: 175
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:
%timeit l = list(map(int, a.split()))
it was 4.07 µs per loop
%timeit l = a.split(' ')
this was 462 ns per loop
%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
Reputation: 16309
I tested the various answers in a jupyter
notebook, and Peter de Rivas does seem to edge out the others proposed.
Interestingly, the mapping to integer seems to be the bottleneck. The str.split()
operation itself is an order of magnitude faster.
Upvotes: 2
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
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