Why doesn't dict have a delete method?

I know there is a remove () method in the python list that removes the given object from the list.

aList = [123, 'xyz', 'zara', 'abc', 'xyz']
aList.remove('xyz')

I know that we can use the del operator to remove an item from a list by offset:

del aList[-1]

I also know that we can use the del operator to remove an item from the dictionary by key:

aDict = {'a':1, 'b':2 'c':3}
del aDict['a']

However, there is no remove () method for the dictionary, which, it seems to me, is beautiful:

aDict.remove('a')

I assume one reason is that remove () does not save any input other than the del instruction for the dictionary, so this is optional. It's right?

So far for the list, remove () combines the "search index by value" and "remove by index" together, so it should be there. Is it correct?

What are other reasons, if any?

+4
4

, list.remove - first. dict - "".

+4

: del, remove pop : ,

remove: ,

del:

pop:

, ( ) del .

+4

pop:

pop ( [, ])
, , . , KeyError.

+3

pop, remove. ? remove . , :

{'a': 5, 'b': 5}

dict.remove . dict.remove(5), ? , . , .


dct.pop(key)

,

value = dct[key]
del dct[key]
return value

pop :

lst.pop(index)

similarly

value = lst[index]
del lst[index]
return value

that is, they both work on the same principle.

+1
source

All Articles