You need to double avoid the backlink:
re.sub('(.+) \(\\1\)', '\\1', 'the (the)')
--> the
Or use the rprefix :
When the prefix "r" or "R" is present, the character following the backslash is included in the line without changes, and all backslashes remain in the line.
re.sub(r'(.+) \(\1\)', r'\1', 'the (the)')
--> the
source
share