I have a list where I save the objects created by a specific class.
I would like to know, cause I can't manage to solve this issue, how do I delete an instance of the class from the list?
This should happen based on knowing one attribute of the object.
You could use a list comprehension:
thelist = [item for item in thelist if item.attribute != somevalue]
This will remove all items with item.attribute == somevalue
.
If you wish to remove just one such item, then use WolframH's solution.
Iterate through the list, find the object and its position, then delete it:
for i, o in enumerate(obj_list):
if o.attr == known_value:
del obj_list[i]
break
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With