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
Post a Comment