If you run dl_distance in a time critical loop and use substitutions, you don't want to import defaultdict and iterate through all subs every time. Instead dl_distance should just take a dict as substitution parameter and let 'key in dict' decide to return value or 1.