In my current work, I use Numpy and list comprehensions a lot and in the interest of the best possible performance I have the following questions:
What actually happens behind the scenes if I create a Numpy array as follows?
a = numpy.array( [1,2,3,4] )
My guess is that python first creates an ordinary list containing the values, then uses the list size to allocate a numpy array and afterwards copies the values into this new array. Is this correct, or is the interpreter clever enough to realize that the list is only intermediary and instead copy the values directly?
Similarly, if i wish to create a numpy array from list comprehension using numpy.fromiter()
:
a = numpy.fromiter( [ x for x in xrange(0,4) ], int )
will this result in an intermediary list of values being created before being fed into fromiter()
?