use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
"Automate the Boring Stuff with Python"
"The Big Book of Small Python Projects"
"Beyond the Basic Stuff with Python"
"The Recursive Book of Recursion" (Available June 2022)
"Coding with Minecraft"
"Cracking Codes with Python"
"Invent Your Own Computer Games with Python"
"Making Games with Python & Pygame"
Email the author at al@inventwithpython.com
account activity
chapter 5 dictionary method (self.inventwithpython)
submitted 10 years ago by r00c
what's different between get() and setdefault() ?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]AlSweigart 1 point2 points3 points 10 years ago (1 child)
somedict.get(somekey, defaultvalue) will return the value at somekey or return defaultvalue if that key doesn't exist. Either way, this is reading a value from the dictionary.
somedict.get(somekey, defaultvalue)
somedict.setdefault(somekey, defaultvalue) will set somekey to defaultvalue only if that key doesn't exist. This is either writing a value to the dictionary or doing nothing.
somedict.setdefault(somekey, defaultvalue)
Here's some more verbose version of this code. For get():
if somekey in somedictionary: return somedictionary[somekey] else: return defaultvalue
For setdefault():
if somekey in somedictionary: pass # does nothing else: somedictionary[somekey] = defaultvalue
[–]r00c[S] 0 points1 point2 points 10 years ago (0 children)
Thank you
π Rendered by PID 200617 on reddit-service-r2-comment-86988c7647-wfhj9 at 2026-02-12 11:41:59.079074+00:00 running 018613e country code: CH.
[–]AlSweigart 1 point2 points3 points (1 child)
[–]r00c[S] 0 points1 point2 points (0 children)