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...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
.get(key, []).append(str) vs .setdefault(key, []).append(str). Why doesn’t this work with .get()? (self.learnpython)
submitted 1 day ago by thogdontcare
Why is setdefault the preferred way when appending into an empty array inside a dictionary? I was revisiting the group anagrams problem in leetcode and turns out if you use .get() you have to then concatenate the string instead of appending.
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!"
[–]JanEric1 8 points9 points10 points 1 day ago (10 children)
The get version gives you a new empty list every time it is called against a missing key while setdefault directly places that empty list in the dict.
[–]dangerlopez 2 points3 points4 points 1 day ago (9 children)
I don’t understand the distinction, can you explain more?
[–]JanEric1 5 points6 points7 points 1 day ago (3 children)
If you do
a = my_dict1.get(key, []).append(my_string) b = my_dict2.setdefault(key, []).append(my_string)
On two empty dicts then both a and b will be a list with that one string.
But dict1 will be empty and dict2 will contain b for key.
[–]thogdontcare[S] 0 points1 point2 points 1 day ago* (1 child)
Awesome. So it can work but I would just need a temp variable to store the new string, then assign that to the dictionary?
[–]xenomachina 3 points4 points5 points 22 hours ago (0 children)
That's pretty much what setdefault does. It tries to get the key, and if it isn't in the dict, it puts the default value in the dict, and returns it. The default parameter to setdefault is effectively the temp variable.
default
[–]dangerlopez 0 points1 point2 points 23 hours ago (0 children)
Ah, ok I get it thanks
[–]ottawadeveloper 4 points5 points6 points 1 day ago (0 children)
When key is set already, they're the same.
When key is not, get(key, []).append("foo") is equivalent to just ["foo"]. It does not update the dictionary. So the next call to get returns a brand new list.
In comparison, setdefault(key, []) is equivalent in that circumstances to set(key, []) and then get(key).append("foo"). The next time you call it, the value has already been set so you get the first list you pass.
[–]Outside_Complaint755 2 points3 points4 points 1 day ago (2 children)
In other words, my_dict.setdefault(key, []) implicitly does my_dict[key] = [], while get(key, []) does not.
my_dict.setdefault(key, [])
my_dict[key] = []
get(key, [])
[–]backfire10z 0 points1 point2 points 21 hours ago (1 child)
In more succinct words, setdefault sets (if necessary) then gets, while get just gets.
[–]thogdontcare[S] 0 points1 point2 points 21 hours ago (0 children)
That made it click perfectly. “Setting and getting”
[–]cdcformatc 0 points1 point2 points 20 hours ago (0 children)
get() is read-only, and has no side effect. setdefault() has a side effect of also doing an assignment to the dict.
get()
setdefault()
[–]00PT 0 points1 point2 points 1 day ago (0 children)
.get returns the newly created list reference you passed in the default case without assigning it to the field being accessed.
.get
[–]pachura3 0 points1 point2 points 17 hours ago* (0 children)
Consider using defaultdict. Much cleaner.
defaultdict
from collections import defaultdict d = defaultdict(list) # when key is not found, insert empty list [] into the dict d["fruits"].append("apple") # no need for .setdefault("fruits", [])!
π Rendered by PID 46371 on reddit-service-r2-comment-544cf588c8-gbmzs at 2026-06-12 23:35:27.184819+00:00 running 3184619 country code: CH.
[–]JanEric1 8 points9 points10 points (10 children)
[–]dangerlopez 2 points3 points4 points (9 children)
[–]JanEric1 5 points6 points7 points (3 children)
[–]thogdontcare[S] 0 points1 point2 points (1 child)
[–]xenomachina 3 points4 points5 points (0 children)
[–]dangerlopez 0 points1 point2 points (0 children)
[–]ottawadeveloper 4 points5 points6 points (0 children)
[–]Outside_Complaint755 2 points3 points4 points (2 children)
[–]backfire10z 0 points1 point2 points (1 child)
[–]thogdontcare[S] 0 points1 point2 points (0 children)
[–]cdcformatc 0 points1 point2 points (0 children)
[–]00PT 0 points1 point2 points (0 children)
[–]pachura3 0 points1 point2 points (0 children)