0

I have this class

class Accounts:
    def __init__(self, id, name, date):
        self.id = id
        self.name = name
        self.date = date

and two lists of this classes

accounts = []
expired_accounts = []

I want to delete elements which are in expired_accounts from accounts (expired_accounts - accounts)

Appending elements

accounts.append(Accounts(id=690000, name="test", date=123456))
print(len(accounts))
expired_accounts(Accounts(id=690000, name="test", date=123456))
print(len(expired_accounts))

Output:

1
1

Now trying to remove elements

test = [x for x in accounts if x not in set(expired_accounts)]
print(len(test))

Output (expecting 0):

1

I tried different solutions from this thread, but neither of them work for my case and I don't know why. Could someone help me, where is the problem?

pomahajbo
  • 1
  • 1
  • Use sets to store objects. Then you can do set1.difference(set2) – Ramsudharsan Manoharan Sep 13 '22 at 07:04
  • 1
    Add the `__eq__` method to be able to compare class instances, needed for the `if x not in ...` check. – ivvija Sep 13 '22 at 07:07
  • 1
    Does this answer your question? [Is there a way to check if two object contain the same values in each of their variables in python?](https://stackoverflow.com/questions/6423814/is-there-a-way-to-check-if-two-object-contain-the-same-values-in-each-of-their-v) – bn_ln Sep 13 '22 at 07:08

0 Answers0