If I have list of objects:
l = [a, b, c]
Then I remove one of those objects:
l.remove(a)
How is python determining which item in the list to remove (under the hood)?
Is it using the memory location of a? (which you can view with hex(id(a)))
It iterates through the list, compares every item with the item to be removed and if it finds a match, it just removes that. It works in O(N). Source: https://wiki.python.org/moin/TimeComplexity
It removes only the first matched item and returns immediately.
If the item to be removed is not there, it fails with
ValueErrorThis is the
listremovefunction which removes the item from a list and it usesPyObject_RichCompareBoolto check if the items are the same. AndPyObject_RichCompareBoolis implemented like thisIf the identity of the objects are the same (if both the objects are the same), then return
1otherwise compare the values and return the result.