mercredi 10 février 2021

how to find and remove object form a list of python object

i am new to python. I am a java programmer. I have a list of Object of a class. My intention is to find and remove an object from the list.

class

class commit:
    def __init__(self, id, message):
        self.id = id
        self.message = message

The code where i am trying to find and delete:

elif command.startswith("git delete"):
        split = command.split();
        deleteID = split[2]; // the id which will be deleted.
        print('deleteID:' + deleteID)
        d1 = {};
        for obj in listCommit:
            print(obj.id)
            if obj.id == deleteID:
                print(obj)
                listCommit.remove(obj);
                d1 = obj;
        # listCommit.remove(d1);    

I have found that this if obj.id == deleteID: is not working.

listCommit.remove(d1); this line showing error that object doesn't exit.

So my question is: how can I find and delete an object form the list?

and this if obj.id == deleteID: checking is giving false, while it should give true.

Aucun commentaire:

Enregistrer un commentaire