Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why dict does not have remove method?

Tags:

python

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?

like image 240
milesma Avatar asked Dec 15 '22 05:12

milesma


1 Answers

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".

like image 156
MisterMiyagi Avatar answered Jan 06 '23 00:01

MisterMiyagi