Say I wanted to create an array (NOT list) of 1,000,000 twos in python, like this:
array = [2, 2, 2, ...... , 2]
What would be a fast but simple way of doing it?
Say I wanted to create an array (NOT list) of 1,000,000 twos in python, like this:
array = [2, 2, 2, ...... , 2]
What would be a fast but simple way of doing it?
Is this what you're after?
# slower.
twosArr = array.array('i', [2] * 1000000)
# faster.
twosArr = array.array('i', [2]) * 1000000
You can get just a list with this:
twosList = [2] * 1000000
-- EDITED --
I updated this to reflect information in another answer. It would appear that you can increase the speed by a ratio of ~ 9 : 1 by adjusting the syntax slightly. Full credit belongs to @john-machin. I wasn't aware you could multiple the array object the same way you could do to a list.
Using the timeit module you can kind of figure out what the fastest of doing this is:
First off, putting that many digits in a list will kill your machine most likely as it will store it in memory.
However, you can test the execution using something like so. It ran on my computer for a long time before I just gave up, but I'm on an older PC:
timeit.Timer('[2] * 1000000').timeit()
Ther other option you can look into is using the array module which is as stated, efficient arrays of numeric values
array.array('i', (2 for i in range(0, 1000000)))
I did not test the completion time of both but I'm sure the array
module, which is designed for number sets will be faster.
Edit: Even more fun, you could take a look at numpy which actually seems to have the fastest execution:
from numpy import *
array( [2 for i in range(0, 1000000)])
Even faster from the comments:
a = 2 * ones(10000000)
Awesmoe!
The currently-accepted answer is NOT the fastest way using array.array
; at least it's not the slowest -- compare these:
[source: johncatfish (quoting chauncey), Bartek]
python -m timeit -s"import array" "arr = array.array('i', (2 for i in range(0,1000000)))"
10 loops, best of 3: 543 msec per loop
[source: g.d.d.c]
python -m timeit -s"import array" "arr = array.array('i', [2] * 1000000)"
10 loops, best of 3: 141 msec per loop
python -m timeit -s"import array" "arr = array.array('i', [2]) * 1000000"
100 loops, best of 3: 15.7 msec per loop
That's a ratio of about 9 to 1 ...