50

In Python, how can I remove an object from a list of objects? Like this:

x = object()
y = object()
array = [x, y]
# Remove x

I've tried array.remove() but it only works with a value, not a specific location in the array. I need to be able to delete the object by addressing its position (remove array[0]).

1
  • The code comment in your post is the answer: it's just array.remove(x), you don't need its position at all. After all, how would you even know x is in position 0 without knowing x? Commented Feb 11, 2023 at 22:21

8 Answers 8

88

There are various ways to delete an object from a list:

my_list = [1,2,4,6,7]

del my_list[1] # Removes index 1 from the list
print my_list # [1,4,6,7]
my_list.remove(4) # Removes the integer 4 from the list, not the index 4
print my_list # [1,6,7]
my_list.pop(2) # Removes index 2 from the list

In your case the appropriate method to use is pop, because it takes the index to be removed:

x = object()
y = object()
array = [x, y]
array.pop(0)
# Using the del statement
del array[0]
Sign up to request clarification or add additional context in comments.

2 Comments

You should update the second part of your answer and have him use .pop(0) since he specifically asks about removing by position.
When using del array[0] is removed` from the list but x itself still exists. Is there a method to remove x simultaneously (for example by deleting x by its pointer)?
7
del array[0]

where 0 is the index of the object in the list (there is no array in python)

Comments

6

If you want to remove multiple object from a list. There are various ways to delete an object from a list

Try this code. a is list with all object, b is list object you want to remove.

example :

a = [1,2,3,4,5,6]
b = [2,3]

for i in b:
   if i in a:
      a.remove(i)

print(a)

the output is [1,4,5,6] I hope, it will work for you

4 Comments

What is link ?
And else: pass is redundant.
@Rafael i'm sorry, variable "link" in there is my mistake. I just to correct it. check = i in a.
yes, no problem using else: pass or no. Thank you for your idea. @Rafael
6

You could try this to dynamically remove an object from an array without looping through it? Where e and t are just random objects.

>>> e = {'b':1, 'w': 2}
>>> t = {'b':1, 'w': 3}
>>> p = [e,t]
>>> p
[{'b': 1, 'w': 2}, {'b': 1, 'w': 3}]
>>>
>>> p.pop(p.index({'b':1, 'w': 3}))
{'b': 1, 'w': 3}
>>> p
[{'b': 1, 'w': 2}]
>>>

Comments

4

If we have an object x (e.g. an instance of some class X) that's in a list of other objects of the same type, we can simply directly remove it using list.remove(), provided the class has __eq__ properly implemented (this is already the case for built-in object types, but for custom classes always remember to implement the eq dunder function):

class X():
    def __init__(self, value=None):
        self.a = value
    def __eq__(self, other):
        if not hasattr(other, 'a'):
           return False
        return self.a == other.a

x = X(4)
y = X(10)
z = X(9)

my_list = [x,y,z]
print([e.a for e in a])
# prints [4, 10, 9]

my_list.remove(x)

print([e.a for e in a])
# prints [10, 9]

Comments

2

You can remove a string from an array like this:

array = ["Bob", "Same"]
array.remove("Bob")

Comments

0

if you wanna remove the last one just do your_list.pop(-1) if you wanna remove the first one your_list.pop(0) or any index you wish to remove

Comments

0

If you know the array location you can can pass it into itself. If you are removing multiple items I suggest you remove them in reverse order.

#Setup array
array = [55,126,555,2,36]
#Remove 55 which is in position 0
array.remove(array[0])

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.