240
笔下虽有千言,胸中实无一策

python: dict.get()

Use dict.get(key[, default]) to assign default values

The code below is functionally equivalent to the original code above, but this solution is more concise.

When get() is called, Python checks if the specified key exists in the dict. If it does, then get() returns the value of that key. If the key does not exist, then get() returns the value specified in the second argument to get().

dictionary = {"message": "Hello, World!"}

data = dictionary.get("message", "")

print(data)  # Hello, World!

  

posted @ 2018-01-11 06:41  CasperWin  阅读(275)  评论(0编辑  收藏  举报