3

I have this basic example to understand the numpy append method.

distances=[]
for i in range (8):
    distances = np.append(distances, (i))
print(distances)

distances=[]
for i in range (8):
    distances.append(i)
print(distances)

The output gives me 2 arrays but are in different format (or what I understand of different format).

[ 0.  1.  2.  3.  4.  5.  6.  7.]
[0, 1, 2, 3, 4, 5, 6, 7]

What is the exact different of both arrays and why is the output different?

5
  • 2
    One is a numpy array containing floats, the other is a list of integers Commented Feb 23, 2018 at 9:34
  • But why does one contain floats and why integers. And why one is an array and the other one is a list, if I am doing the same thing? And iis the same for both, I do not understand why one would be floats and the other one ints.@DavidG Commented Feb 23, 2018 at 9:39
  • Floats are the default type for numpy arrays. Commented Feb 23, 2018 at 9:40
  • What do you mean "doing the same thing"? You are not. The first gives you an array and is the result of array methods, the second produces a list and is a result of list methods. Commented Feb 23, 2018 at 9:44
  • np.append is a numpy function that is named like the list append, but is a poor replica. I discourage using it. It gives beginners too many problems (like 4-5 SO questions in the last couple of days). Commented Feb 23, 2018 at 17:59

3 Answers 3

4

Your second method is pure python and doesn't use any numpy, so the type starts as list ([]) and stays that way, because list.append() leaves list as a list. It contains integers because that's what you get out of range and nothing in your code changes them.

The first method uses numpy's append method that returns an ndarray, which uses floats by default. This also explains why your returned array contains floats.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much!
list.append() returns None, not a list.
1

The first code

distances=[]
for i in range (8):
    distances = np.append(distances, (i))
print(distances)

results in distances being an array of floats. While the second code

distances=[]
for i in range (8):
    distances.append(i)
print(distances)

results in distances being a list of ints.

arrary is a numpy type (main difference: faster, all items have the same type), while list is python-internal (main difference: works without numpy, can hold any mixed types).

Comments

1

The first gives you an numpy.ndarray and is the result of numpy methods, the second produces a list and is a result of list methods. Numpy arrays and Python lists are not the same thing.

Numpy arrays are essentially object-oriented wrappers around fix-sized, typed, true multidimensional arrays. numpy array methods are optimized for vectorized numerical calculations, and along with scipy, provide powerful scientific computing and linear algebra capabilities.

Python list objects are heterogeneous, resizable, array-lists. They are optimized for constant-time .append. Indeed, both of these for-loops will scale very differently. numpy.ndarray.append requires creating an entirely new array each iteration. Python list have amoratized constant time append. Thus, you will see quadratic growth in runtime as the size of your numpy.ndarray scales, whereas with the list, you will see linear scaling.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.