Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have lists (and list of lists) as standard data-structure (most of my functions returns list/list of lists). But now I have to use some Numpy functions. Do I have to convert all lists to numpy array before using any function from numpy/scipy. For example

>>>x= [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 
>>>y= [ 1, 10, 20]
>>>dot(x,y)
#array([ 81, 174, 267])
>>>X_np=array(x)
>>>y_np=array(y)
>>>dot(x_np,y_np)
#array([ 81, 174, 267])

As in this case dot(x,y) and dot(x_np,y_np) gave same result, so using list instead of numy array doesn't make difference....but is this the case for all numpy/scipy functions or I have to do my_np_array= array(my_list) before calling any/all numpy/scipy functions?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Almost all of the numpy/scipy functions will convert the input to an array before operating on it. But why trust me? Just try whichever ones you need and see if it works.

The exception will be any functions which modify an array in place like np.put for example.

>>> np.put([1,2,3],[0,1],[2,1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 394, in put
    return a.put(ind, v, mode)
AttributeError: 'list' object has no attribute 'put'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.