[Numpy-discussion] appending/inserting/deleting axes of arrays

Bill Baxter wbaxter at gmail.com
Thu Jul 20 22:12:26 CDT 2006

Is there any nicer syntax for the following operations on arrays?

Append a row:
a = vstack((a,row))

Append a column:
a = hstack((a,col))

Append a row of zeros:
a = vstack((a,zeros((1,a.shape[1]))))

Append a col of zeros:
a = hstack((a,zeros((a.shape[0],1))))

Insert a row before row j
a = vstack(( a[:j], row, a[j:] ))

Insert a column before col j
a = hstack(( a[:j], col, a[j:] ))

Insert a row of zeros before row j
a = vstack(( a[:j], zeros((1,a.shape[1])), a[j:] ))

Insert a column of zeros before col j
a = hstack(( a[:j], zeros((a.shape[0],1)), a[j:] ))

Delete row j:
a = vstack(( a[:j], a[j+1:] ))

Delete col j:
a = hstack(( a[:j], a[j+1:] ))

...And in more general the same types of operations for N-d arrays.

I find myself using python lists of lists a lot just for the easy
readability of a.append(row) compared to a = vstack((a,row)).

I guess, though, if I'm building an array by appending a row at a
time, then maybe it *is* better to use a python list o list for that?
Then each 'append' only copies pointers of the existing rows rather
than copying the data in each row.  Is that correct?  Also do python
lists over-allocate in order to avoid having to re-allocate and copy
every time there's an append()?  Numpy arrays don't over-allocate I


More information about the Numpy-discussion mailing list