Dictionary pop() method Python

The Python dictionary pop() method is similar to popitem() method but unlike the popitem() method which remove random item,the pop() method can be used to remove a specific item from the dictionary.

Link :Dictionary popitem() method

The pop() method can accept one or two arguments.If you pass one argument it has to be the key of the item which you would want to remove from the dictionary.If the key passed does not exist in the dictionary you will get an error stating “keyerror: key_passed” (key_passed is the non-existent key).

Note once the item is removed from the dictionary the method pop() returns the ‘value’ of the ‘key’ which has been removed.

Link :Python dictionary data type

>>> d={ 'mana':'mama' , 'ruru':'luli' , 'kuku':'Pupi' , '545':'10810'}
>>> d.pop( 'ruru' )
'luli'
>>> d
{'mana': 'mama', 'kuku': 'Pupi', '545': '10810'}
>>> d.pop('Mama') #Passing non-existent key
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    d.pop('Mama')
KeyError: 'Mama'
>>> d.pop( 'kuku' )
'Pupi'
>>> d
{'mana': 'mama', '545': '10810'}

The next section discuss how to pass two arguments.


Passing two arguments to pop()

If you pass two arguments:
 
i)the first argument has to be the key of the item which you would want to remove from the dictionary and
 
ii)the second argument is the value of the item which you would want to remove from the dictionary.
 
Now there are two cases here which are discuss below.

If the first argument is a valid key

If the first argument we pass is a valid key or existent key.In this case it doesn’t matter whether the second argument is valid (meaning it forms a valid key:value pair with the first argument) or not the pop() method will remove the item having the key as the first argument.

>>> d={ 'mana':'mama' , 'ruru':'luli' , 'kuku':'Pupi' , '545':'10810'}
>>> d.pop( '545' , '10810' ) #valid first and second argument
'10810'
>>> d #item with '545' key is removed 
{'mana': 'mama', 'ruru': 'luli', 'kuku': 'Pupi'}
>>> d.pop('ruru' , 'LULU') #valid first argument but not 2nd
'luli'
>>> d #item with 'ruru' key is removed
{'mana': 'mama', 'kuku': 'Pupi'}

So you can see even if the second argument is valid or not,if the first argument is valid then the item is removed by all means.

The first argument is invalid

If the first argument is non-existent then whether the second argument is valid or not any item will not be removed from the dictionary.And in this case you will not get any error message instead the pop() method will return the second argument.

>>> d={ 'mana':'mama' , 'ruru':'luli' , 'kuku':'Pupi' , '545':'10810'}
>>> d.pop( 'Mana' , 'mama' ) #valid 2nd argument
'mama'
>>> d #no item removed
{'mana': 'mama', 'ruru': 'luli', 'kuku': 'Pupi', '545': '10810'}
>>> d.pop( 'ruRu' , 'LULI' ) #invalid 2nd argument
'LULI'
>>> d #No item removed
{'mana': 'mama', 'ruru': 'luli', 'kuku': 'Pupi', '545': '10810'}

The use of passing second argument to pop() method is to avoid getting any error message even if the first argument may turn out to be invalid.This can preserve any smooth work flow of the program as the error message may disrupt the program sometimes if not handle correctly.



Leave a Reply

Your email address will not be published. Required fields are marked *