.get(key, []).append(str) vs .setdefault(key, []).append(str). Why doesn’t this work with .get()?

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.

Posted Jun 11, 2026, 10:26 PM UTC

613
Loading comments…