I know python list has a remove() method, which remove the given object from the list.
aList = [123, 'xyz', 'zara', 'abc', 'xyz']
aList.remove('xyz')
I know that we can use del statement to delete an item from the list by offset:
del aList[-1]
I also know that we can use del statement to delete an item from the dictionary by key:
aDict = {'a':1, 'b':2 'c':3}
del aDict['a']
However, there is no remove() method for a dictionary, which I think is perfectly fine:
aDict.remove('a')
I guess one reason is that remove() does not save any typing than del statement for dictionary, so it is not necessary. Is this correct?
While for list, remove() combines "search index by value" and "delete by index" together, so it has to be there. Is this correct?
What are the other reasons if any?
Note that list.remove
makes a very strict guarantee - removing the first occurrence of a value. Now, the ordering in a dict
is arbitrary - there is no "first".
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