import re


def clean_links(wikilinks, language, prefixes):
    """Removing links that are not in the main namespace, contain the
    chars "{}[]<>", are linking to sections on the same page or are
    already localized.

    Args:
      wikilinks (iterable[Wikilink]): Iterable of the wikilinks to clean.
      language (str): ISO code of the language.
      prefixes (iterable[str]): List of prefixes to exclude.

    Returns:
      set: Cleaned wikilinks."""
    wikilinks = {
        link for link in wikilinks
        if
        not re.match(  # In main namespace
            ":?(category|file|image|media|{}):".format("|".join(prefixes)),
            link.title, flags=re.I
        ) and
        not any(x in link.title for x in "{}[]<>") and  # Special chars
        not link.title.startswith("#") and  # In-page links
        not "/{}".format(language) in link.title  # Localized links
        }

    return wikilinks