0

I want to add something to a ndarray, what am I doing wrong?

import numpy as np
sequence =np.repeat(1, 4)
print(sequence)
np.append(sequence, 7)
print(sequence)

Expected result in console: [1 1 1 1] [1 1 1 1 7]

Actual result: [1 1 1 1] [1 1 1 1]

4
  • 1
    sequence = np.append(sequence, 7) Commented Jan 8, 2021 at 21:38
  • 1
    It doesn't do it in-place, so you have to access the return value of np.append Commented Jan 8, 2021 at 21:39
  • np.append is not a list append clone. Do not use as such; better yet don't use it at all! Also you can't add an element to just one row of a 2d array. Again, arrays are not the same as nested lists. Commented Jan 8, 2021 at 21:58
  • It's not in-place! Of course! Thank you you guys! Commented Jan 9, 2021 at 0:01

1 Answer 1

1

np.append() returns a new list, so what you wanna do is you wanna do sequence = np.append(sequence, 7)

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

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.