The del statement and pop method to delete key-value pairs in the python dictionary

The del statement and pop method to delete key-value pairs in the dictionary have the same effect as the pop() function. The pop() function has a return value, but the del statement has no return value. pop() function syntax: pop(key[,default]), where key: the key value to be deleted, default: if there is no key, return the default value

if __name__ == '__main__':
    dic = {"张三": 24, "李四": 23, "王五" : 25, "赵六" : 27}
    del dic["张三"]
    print(dic)

    dic.pop("李四")
    print(dic)

 

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/115221355