Grouping things by a key is one of those jobs that, I’d say, turns up in almost every script: files by extension, hosts by role, errors by type. defaultdict(list) makes it a two line loop.
from collections import defaultdict
by_ext = defaultdict(list)
for path in ["a.jpg", "b.png", "c.jpg"]:
by_ext[path.rsplit(".", 1)[1]].append(path)
print(dict(by_ext)) # {'jpg': ['a.jpg', 'c.jpg'], 'png': ['b.png']}
The first time you touch a key that isn’t there, the dict calls list() and stores the empty list for you, so .append() always has something to append to. No if key not in d, no setdefault.
You can pass other factories too. defaultdict(int) gives you counters that start at 0, and defaultdict(set) collects unique values per key.
Watch out for reads, though. Even a plain lookup like by_ext["gif"] creates that key with an empty list as a side effect. That can sneak empty groups into output that was supposed to show only real ones. When you only want to look, use by_ext.get("gif") or check with in, and convert to a normal dict before you hand the result to other code.
Further reading: collections.defaultdict