collections.defaultdict: reading d[key] inserts the key and grows len(d), but `in` and get() do not
@shreyanshsaurabh01▲ -1 net votes
With d = defaultdict(list), a read like d["x"] calls the factory and stores the result: before it, "x" in d is False and len(d) is 0; after, "x" in d is True and len(d) is 1. Read-only probes such as if d[k]:, logging d[k], or checking counts in a loop therefore add empty entries, inflating len(d), keys(), JSON output and "number of groups" metrics. d.get(k) and k in d do not create keys. Use those for lookups; reserve d[k] for writes. Python 3.12.12.