numpy-equivalent of list.pop?
Solution 1
There is no pop
method for NumPy arrays, but you could just use basic slicing (which would be efficient since it returns a view, not a copy):
In [104]: y = np.arange(5); y
Out[105]: array([0, 1, 2, 3, 4])
In [106]: last, y = y[-1], y[:-1]
In [107]: last, y
Out[107]: (4, array([0, 1, 2, 3]))
If there were a pop
method it would return the last
value in y
and modify y
.
Above,
last, y = y[-1], y[:-1]
assigns the last value to the variable last
and modifies y
.
Solution 2
Here is one example using numpy.delete()
:
import numpy as np
arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print(arr)
# array([[ 1, 2, 3, 4],
# [ 5, 6, 7, 8],
# [ 9, 10, 11, 12]])
arr = np.delete(arr, 1, 0)
print(arr)
# array([[ 1, 2, 3, 4],
# [ 9, 10, 11, 12]])
Solution 3
There isn't any pop()
method for numpy arrays unlike List, Here're some alternatives you can try out-
- Using Basic Slicing
>>> x = np.array([1,2,3,4,5])
>>> x = x[:-1]; x
>>> [1,2,3,4]
- Or, By Using
delete()
Syntax - np.delete(arr, obj, axis=None)
arr
: Input array
obj
: Row or column number to delete
axis
: Axis to delete
>>> x = np.array([1,2,3,4,5])
>>> x = x = np.delete(x, len(x)-1, 0)
>>> [1,2,3,4]
Solution 4
Pop doesn't exist for NumPy arrays, but you can use NumPy indexing in combination with array restructuring, for example hstack/vstack or numpy.delete(), to emulate popping.
Here are some example functions I can think of (which apparently don't work when the index is -1, but you can fix this with a simple conditional):
def poprow(my_array,pr):
""" row popping in numpy arrays
Input: my_array - NumPy array, pr: row index to pop out
Output: [new_array,popped_row] """
i = pr
pop = my_array[i]
new_array = np.vstack((my_array[:i],my_array[i+1:]))
return [new_array,pop]
def popcol(my_array,pc):
""" column popping in numpy arrays
Input: my_array: NumPy array, pc: column index to pop out
Output: [new_array,popped_col] """
i = pc
pop = my_array[:,i]
new_array = np.hstack((my_array[:,:i],my_array[:,i+1:]))
return [new_array,pop]
This returns the array without the popped row/column, as well as the popped row/column separately:
>>> A = np.array([[1,2,3],[4,5,6]])
>>> [A,poparow] = poprow(A,0)
>>> poparow
array([1, 2, 3])
>>> A = np.array([[1,2,3],[4,5,6]])
>>> [A,popacol] = popcol(A,2)
>>> popacol
array([3, 6])
Solution 5
The most 'elegant' solution for retrieving and removing a random item in Numpy is this:
import numpy as np
import random
arr = np.array([1, 3, 5, 2, 8, 7])
element = random.choice(arr)
elementIndex = np.where(arr == element)[0][0]
arr = np.delete(arr, elementIndex)
For curious coders:
The np.where() method returns two lists. The first returns the row indexes of the matching elements and the second the column indexes. This is useful when searching for elements in a 2d array. In our case, the first element of the first returned list is interesting.
Anton Alice
Updated on August 31, 2021Comments
-
Anton Alice over 2 years
Is there a numpy method which is equivalent to the builtin
pop
for python lists?Popping obviously doesn't work on numpy arrays, and I want to avoid a list conversion.
-
Zeugma over 7 yearspop doesn't exist in numpy and by design it is not recommended to emulate it. You would better approach the algorithm you need to write without using a pop pattern
-
-
parvij about 4 yearspop returns the value and the list become shorter
-
Brambor almost 4 yearsBut
list.pop
can take an index as a parameter. This won't do. -
sameer_nubia over 3 yearsCan you explain this l1=[10,11,12,13,14,16,17,18] [l1.pop(l1.index(i)) for i in l1 if i%2==0] print("l1:",l1) output - l1: [11, 13, 16, 17]