Validating Email Addresses With a Filter

You are given an integer N followed by N email addresses. Your task is to print a list containing only valid email addresses in lexicographical order.

Valid email addresses must follow these rules:

  • It must have the username@websitename.extension format type.
  • The username can only contain letters, digits, dashes and underscores [a-z], [A-Z], [0–9],[_-].
  • The website name can only have letters and digits [a-z], [A-Z], [0–9].
  • The extension can only contain letters [a-z], [A-Z].
  • The maximum length of the extension is 3.


A filter takes a function returning True or False and applies it to a sequence, returning a list of only those members of the sequence where the function returned True. A Lambda function can be used with filters.

Let’s say you have to make a list of the squares of integers from 0 to 9 (both included).

>> l = list(range(10))
>> l = list(map(lambda x:x*x, l))

Now, you only require those elements that are greater than 10 but less than 80.

>> l = list(filter(lambda x: x > 10 and x < 80, l))

Easy, isn’t it?


Complete the function fun in the editor below.

fun has the following parameters:

  • string s: the string to test


  • boolean: whether the string is a valid email or not

Input Format

The first line of input is the integer , the number of email addresses.
lines follow, each containing a string.


Each line is a non-empty string.

Sample Input


Sample Output

['', '', '']


import re
def fun(s):
a = re.match(r'[a-zA-Z0-9_-]+@[a-zA-Z0-9]+\.[a-zA-Z]{1,3}$', s)
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)





My homepage to record my thought processes for solving SQL and Algorithm questions

Recommended from Medium

A Modern Architecture for Testing BLE Devices

Legacy Java 6 Runtime For Macos Mojave

The Figment Learn Notes

Laser Cooldown System

Haskell :: (.) . (.) Explained

Why do I still use PowerPoint when there are plenty of free alternatives to PowerPoint?

How to implement an API first design methodology

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


In love with telling stories with data

More from Medium

Difference between SQL and SQLite

Introducing the unit testing in Python

Hackerrank — Sherlock and the Valid String walkthrough #Python #Hackerrank

Prefix Tree Pokédex