Input Format
The first line of input is the integer , the number of email addresses.
 lines follow, each containing a string.
Constraints
Each line is a non-empty string.
Output Format
Output a list containing the valid email addresses in lexicographical order. If the list is empty, just output an empty list, [].

hackerrank validating email address with a filter problem solution in python

Problem solution in Python 3 programming language.

import re
lst = list()
for i in range(int(input())):
    lst.append(input())
print(sorted(list(filter(lambda x: re.search(r'^[\w\d-]+@[A-Za-z0-9]+\.\w?\w?\w$',x),lst))))

Second solution

def fun(email):
    try:
        username, url = email.split("@")
        website, extension = url.split(".")
    except ValueError:
        return False
    if username.replace("-""").replace("_""").isalnum() is False:
        return False
    elif website.isalnum() is False:
        return False
    elif len(extension) > 3:
        return False
    else:
        return True
def filter_mail(emails):
    return list(filter(fun, emails))
if __name__ == '__main__':
    n = int(input())
    emails = []
    for _ in range(n):
        emails.append(input())
filtered_emails = filter_mail(emails)
filtered_emails.sort()
print(filtered_emails)

Problem solution in Python 2 programming language.

import re
lst = list()
for i in range(int(raw_input())):
    lst.append(raw_input())
print sorted(list(filter(lambda x: re.search(r'^[\w\d-]+@[A-Za-z0-9]+\.\w?\w?\w$',x),lst)))