import re


def extract_top_level_domain_from_uri(uri):
    """
    Extract host's top level domain from URIs like 'https://amy.carpentries.org/api/v1/organizations/earlham.ac.uk/' to 'earlham.ac.uk'.
    When subdomains are used, as in 'https://amy.carpentries.org/api/v1/organizations/cmist.manchester.ac.uk/' we are only interested in
    top level domain 'manchester.ac.uk'.
    :param uri: URI like 'https://amy.carpentries.org/api/v1/organizations/earlham.ac.uk/'
    :return: top level domain like 'earlham.ac.uk'
    """
    host = list(filter(None, re.split("(.+?)/", uri)))[-1]  # Get the host from the URI first
    # Now just get the top level domain of the host
    domain_parts = list(filter(None, re.split("(.+?)\.", host)))
    if len(domain_parts) >= 3:
        domain_parts = domain_parts[-3:]  # Get the past 3 elements of the list only
    top_level_domain = ''.join((x + '.') for x in domain_parts)  # join parts with '.' in between
    top_level_domain = top_level_domain[:-1]  # remove the extra '.' at the end after joining
    return top_level_domain