-1

I have a list of objects of the following form

a = [<__main__.Card at 0x10b47a630>,
     <__main__.Card at 0x10b47aba8>,
     <__main__.Card at 0x10b47ac18>,
     <__main__.Card at 0x10b47a588>,
     <__main__.Card at 0x10b47a0f0>,
     <__main__.Card at 0x10b47a208>]

I can call a single object from the list;

print(a[0]) prints <__main__.Card at 0x10b47a630>

I would like to remove one of the objects from the list, but not using the index, but by calling the object itself.

a.remove(<__main__.Card at 0x10b47a630>)  # I think that the format in the bracket is wrong

I know that I can do this with strings:

a = ['a', 'b', 'c', 'z']

a.remove('c')

a = ['a', 'b', 'z']

However, I have not figured out how to do it in this case.

I know that I can do it like this:

a.remove(a[0])

but I am not sure the form of the object that should go in .remove().

Luka Vlaskalic
  • 445
  • 1
  • 3
  • 19
  • 1
    You need to reference the object somehow, like you always would, because by default your class will use identity for equality. You could define equality some other way, and pass an equal object (what is essentialyl happening with your `a.remove('c')` example – juanpa.arrivillaga Mar 16 '20 at 20:23
  • 4
    What are the criteria for removing an object? You need to distinguish somehow. – a_guest Mar 16 '20 at 20:24
  • why not `a.pop(0)` if you know the position? – Jean-François Fabre Mar 16 '20 at 20:25
  • @Jean-FrançoisFabre He doesn't know the position, that was just an example. – Barmar Mar 16 '20 at 20:25
  • See https://stackoverflow.com/questions/7270321/finding-the-index-of-elements-based-on-a-condition-using-python-list-comprehensi – Barmar Mar 16 '20 at 20:27
  • Do you have a variable containing the object you want to remove? Then you can use `a.remove(that_variable)` – Barmar Mar 16 '20 at 20:27

2 Answers2

1

Assuming that

a = [<__main__.Card at 0x10b47a630>,
     <__main__.Card at 0x10b47aba8>,
     <__main__.Card at 0x10b47ac18>,
     ...

is the output of print a, then <__main__.Card at 0x10b47aba8> is just the default string representation of the object, not really anything that you can use to reference the object in code.

If the object you want to delete has some parameter that distinguishes it then you can do:

obj_to_remove = [obj for obj in a if obj.birthmark == "a mole"][0]
a.remove(obj_to_remove)

where obj.birthmark is the distinguishing characteristic and "a mole" is the value that marks the object you want to remove.

alternatively, you can use the list comprehension above to remove the object without using remove:

a = [obj for obj in a if obj.birthmark != "a mole"]
rcriii
  • 687
  • 6
  • 9
0

any variable that has assigned an element from a list of objects has actually a reference to that object, here is an example how you can remove an object from a list of objects without using the index:

from random import choice


class Test:
    def __init__(self, x):
        self.x = x
    def __repr__(self):
        return f'Test(x={self.x})'

l = [Test(i) for i in range(5)]
print(l)

obj = choice(l)
print(obj)
l.remove(obj)
print(l)

output:

[Test(x=0), Test(x=1), Test(x=2), Test(x=3), Test(x=4)]
Test(x=1)
[Test(x=0), Test(x=2), Test(x=3), Test(x=4)]
kederrac
  • 16,819
  • 6
  • 32
  • 55