Your question got me wondering so I wrote a basic function to compare their timings. And it seems you are right! Timings change but only a little. Here you can see the code below and the output.
import numpy as np
import time
import random
def stack_question():
start=time.time()*1000
array_probabilities = [0.5 for _ in range(4)]
a = [random.choices([0, 1], weights=[1 - probability, probability])[0] for probability in array_probabilities]
end=time.time()
return (start-end)
def numpy_random_array():
start_time=time.time()*1000
val=np.random.rand(4,1)
end_time=time.time()
return (start_time-end_time)
print("List implementation ",stack_question())
print("Array implementation ",numpy_random_array())
The output:
List implementation 1665476650232.8433
Array implementation 1665476650233.9226
Edit: From geeks4geeks I found the following explanation of why it is faster to use numpy arrays.
NumPy Arrays are faster than Python Lists because of the following reasons:
An array is a collection of homogeneous data-types that are stored in contiguous memory locations. On the other hand, a list in Python is a collection of heterogeneous data types stored in non-contiguous memory locations.
The NumPy package breaks down a task into multiple fragments and then processes all the fragments parallelly.
The NumPy package integrates C, C++, and Fortran codes in Python. These programming languages have very little execution time compared to Python.