0

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]]
1
  • 1
    These are lists, not arrays. Commented Jun 13, 2020 at 16:56

3 Answers 3

1
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)
Sign up to request clarification or add additional context in comments.

Comments

0

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]]

1 Comment

Thanks. I haven't used python in some time, and was under the mistaken impression that remove() would return a list with the object removed.
0

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)

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.