from re import match
from re import split


def _list_of_countries(value):
    """
    Parses a comma or semicolon delimited list of ISO 3166-1 alpha-2 codes,
    discarding those which don't match our expected format. We also allow a
    special pseudo-country code "iso".

    Returns a list of lower-case, stripped country codes (plus "iso").
    """


    countries = []
    candidates = split('[,;]', value)

    for candidate in candidates:
        # should have an ISO 3166-1 alpha-2 code, so should be 2 ASCII
        # latin characters.
        candidate = candidate.strip().lower()
        if candidate == 'iso' or match('[a-z][a-z]', candidate):
            countries.append(candidate)

    return countries