def get_overlap(keys):
    prefixes = defaultdict(set)
for key in keys:
    for prefix in [key[:i] for i in range(len(key))]:
        prefixes[prefix].add(key)
overlap = set()
for key in keys:
    for suffix in [key[i:] for i in range(len(key))]:
        overlap.update([(key, other) for other in prefixes[suffix] if other !=
            key])
return overlap
Получение всех ключей в словаре, которые перекрываются с другими ключами в том же словаре