I am trying to remove a key and value from an OrderedDict
but when I use:
dictionary.popitem(key)
it removes the last key and value even when a different key is supplied. Is it possible to remove a key in the middle if the dictionary?
I am trying to remove a key and value from an OrderedDict
but when I use:
dictionary.popitem(key)
it removes the last key and value even when a different key is supplied. Is it possible to remove a key in the middle if the dictionary?
Yes, you can use
del
:Below is a demonstration:
In fact, you should always use
del
to remove an item from a dictionary.dict.pop
anddict.popitem
are used to remove an item and return the removed item so that it can be saved for later. If you do not need to save it however, then using these methods is less efficient.