Max
Max

Reputation: 725

Using remove() to delete array within an array

If I have an array of arrays (a matrix) in python, e.g.

my_array = [[1,2,3],[4,5,6],[7,8,9],[1,2,3]]

and I would like to remove all instances of [1,2,3]

new_array = my_array.remove([1,2,3])

results in null. Is there a way to apply remove() to arrays within arrays in this way that would work, i.e. so that

new_array = [[4,5,6],[7,8,9]]

Upvotes: 0

Views: 210

Answers (3)

Mukul Kumar
Mukul Kumar

Reputation: 2103

my_array = [[1,2,3],[4,5,6],[7,8,9],[1,2,3]]

# you can put your conditions
new_array = [arr for arr in my_array if [1,2,3] != arr]

print(new_array)

Upvotes: 1

ksohan
ksohan

Reputation: 1203

You can use a loop and remove function to get the desired result

my_array = [[1,2,3],[4,5,6],[7,8,9],[1,2,3]]

while([1,2,3] in my_array):
    my_array.remove([1,2,3])

print(my_array)

Upvotes: 0

Dhaval Taunk
Dhaval Taunk

Reputation: 1672

The remove() doesn't return any value (returns None). Also, you have defined a list of list, not array. You should write it this way:-

my_array = [[1,2,3],[4,5,6],[7,8,9],[1,2,3]]
count = my_array.count([1,2,3])  # returns 2
for i in range(count):
    my_array.remove([1,2,3])

print(my_array)

Output:-

[[4, 5, 6], [7, 8, 9]]

Upvotes: 0

Related Questions