Search code examples
pythondictionarydel

Python del if in dictionary in one line


Is there a one line way of doing the below?

myDict = {}
if 'key' in myDic:
    del myDic['key']

thanks


Solution

  • You can write

    myDict.pop(key, None)