How to remove a key from a python dictionary? -


when trying delete key dictionary, write:

if 'key' in mydict:     del mydict['key'] 

is there 1 line way of doing this?

use dict.pop():

my_dict.pop('key', none) 

this return my_dict[key] if key exists in dictionary, , none otherwise. if second parameter not specified (ie. my_dict.pop('key')) , key not exist, keyerror raised.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -