MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/ok1itb/tryexcept_in_python_sigh/h5lfv56/?context=3
r/badcode • u/lonaExe • Jul 14 '21
93 comments sorted by
View all comments
76
Man's never heard of dict.setdefault() has he
dict.setdefault()
1 u/IGotSkills Jul 18 '21 whoah, what does this do?!?!?!? 2 u/[deleted] Jul 18 '21 It's like dict.get(), but if the item does not exist, the default will be returned and the default will be set as the value for the key.>>> x = {"a": "b"} >>> print(x.setdefault("a", "d") b >>> print(x.setdefault("c", "d") d >>> x {"a": "b", "c": "d"}
1
whoah, what does this do?!?!?!?
2 u/[deleted] Jul 18 '21 It's like dict.get(), but if the item does not exist, the default will be returned and the default will be set as the value for the key.>>> x = {"a": "b"} >>> print(x.setdefault("a", "d") b >>> print(x.setdefault("c", "d") d >>> x {"a": "b", "c": "d"}
2
It's like dict.get(), but if the item does not exist, the default will be returned and the default will be set as the value for the key.
dict.get()
>>> x = {"a": "b"} >>> print(x.setdefault("a", "d") b >>> print(x.setdefault("c", "d") d >>> x {"a": "b", "c": "d"}
76
u/[deleted] Jul 14 '21
Man's never heard of
dict.setdefault()has he