import re


def extract_names(filename):
    """
    Given a single file name for babyXXXX.html, returns a
    single list starting with the year string followed by
    the name-rank strings in alphabetical order.
    ['2006', 'Aaliyah 91', 'Aaron 57', 'Abagail 895', ...]
    """
    names = []
    temp_dict = {}
    with open(filename) as f:
        scrape_names = f.read()
    year_pattern = re.compile(r'Popularity\sin\s(\d\d\d\d)')
    name_pattern = re.compile(r'<td>(\d+)</td><td>(\w+)</td><td>(\w+)</td>')
    year_result = year_pattern.findall(scrape_names)
    name_result = name_pattern.findall(scrape_names)
    names.extend(year_result)
    for name in name_result:
        (rank, male, female) = name
        if male not in temp_dict:
            temp_dict[male] = rank
        if female not in temp_dict:
            temp_dict[female] = rank
    for k, v in temp_dict.items():
        names.append(f'{k} {v}')
    names.sort()
    return names