How to add an integer to python array?

528    Asked by AryanKhan in Python , Asked on Apr 17, 2021

I am trying to add an integer to an already existing array. But I am getting error “numpy.ndarray' object has no attribute 'append'”:

 "AttributeError: 'numpy.ndarray' object has no attribute 'append'"

This is the code I am using:

import numpy as np

data = np.array(f['abc']['efg']['xyz'])

data.append(0)

Answered by Aryan Khan

You have defined a numpy array and trying to append data using a normal python method which is not applicable on numpy arrays. Try using numpy append method:

  data = np.append(data, 0)

Hope this will resolve your issue!



Your Answer

Interviews

Parent Categories