Timeline for Efficient way to create list of same numbers?
Current License: CC BY-SA 3.0
7 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 31, 2013 at 2:55 | comment | added | Warren Weckesser |
@Jaime: Also note that empty + fill is faster than repeat
|
|
| Mar 31, 2013 at 2:46 | comment | added | Jaime |
To honor the numpy tag, a = np.empty((elements,), dtype=np.int); a.fill(number) is much faster than [number] * elements for higher values of elements. But the return is not a real list.
|
|
| Mar 30, 2013 at 23:15 | vote | accept | lord12 | ||
| Mar 30, 2013 at 23:12 | history | edited | Pavel Anossov | CC BY-SA 3.0 |
added 187 characters in body
|
| Mar 30, 2013 at 23:08 | comment | added | Gareth Latty | Note that this may have unexpected results (depending on how much you know of how Python variables work) with mutable types - it produces a list of references to the same object. | |
| Mar 30, 2013 at 23:05 | comment | added | DSM |
The question is tagged numpy too, so maybe mention numpy.repeat(1, 10)? (Although it'll be slower than multiplying a list until n is really big.)
|
|
| Mar 30, 2013 at 23:01 | history | answered | Pavel Anossov | CC BY-SA 3.0 |