# Hackerrank Solution: Validating Email With a filter in Python

Written By - Bashir Alam
Topics we will cover

## Question: Validating Email Addresses With a Filter - Hacker Rank (Python Functionals)

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.

• 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.

Concept:

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))``

Example:

Complete the functionÂ funÂ in the editor below.
fun has the following parameters:

• string s:Â the string to test

Returns:

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

Input format:

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

Constraints:

Each line is a non-empty string.

Input sample:

```3
lara@hackerrank.com
brian-23@hackerrank.com
britts_54@hackerrank.com```

Output sample:

`['brian-23@hackerrank.com', 'britts_54@hackerrank.com', 'lara@hackerrank.com']`

## Possible solutions

We will now use multiple ways to solve this question. The following Python code is already given in the Hacker Rank.

``````def fun(s):
# return True if s is a valid email, else return False

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)``````

So, we have to build the `fun()` method. We will use the following ways to solve the question.

### Solution-1: Using try and except blocks

We will now use the try and except block to check if the given email address is valid or not and then change accordingly. The try block lets you test a block of code for errors. The except block lets you handle the error. Let us start with the coding part.

``````def fun(email):

#using try block
try:

# splitting the name and url of the email adress
website, extension = url.split('.')

# raise error if the email is not valied
except ValueError:
return False

# we are not replacing the - and _
if username.replace('-', '').replace('_', '').isalnum() is False:
return False

# checking if all characters are alphabets and numerics
elif website.isalnum() is False:
return False

# checking if the length is less than 3
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)``````

Input:

```3
lara@hackerrank.com
brian-23@hackerrank.com
britts_54@hackerrank.com```

Output:

`['brian-23@hackerrank.com', 'britts_54@hackerrank.com', 'lara@hackerrank.com']`

As you can see, the email addresses were verified by Python code.

### Solution-2: Using re.compile method

We will now use the regular expressions to validate the email addresses. A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression (or if a given regular expression matches a particular string, which comes down to the same thing).Â  We will use the compile() method, which can combine a regular expression pattern into pattern objects, which can be used for pattern matching. It also helps to search for a pattern again without rewriting it.

This solution requires you to have a better understanding of regualar expressions. Let us start the coding.

``````# importing the regular expression module
import re

def fun(s):
# defining the patterns
pattern = re.compile("^[\\w-]+@[0-9a-zA-Z]+\\.[a-z]{1,3}\$")

# returing if the email is valid
return pattern.match(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)``````

Input:

```3
lara@hackerrank.com
brian-23@hackerrank.com
britts_54@hackerrank.com```

Output:

`['brian-23@hackerrank.com', 'britts_54@hackerrank.com', 'lara@hackerrank.com']`

As you can see, the emails were verified using regular expressions.

### Solution-3: Using re.match method

We will now use the match() to validate the email addresses. The match method returns a corresponding match object instance if zero or more characters at the beginning of the string match the regular expression pattern. In simple words, the re.match() returns a match object only if the pattern is located at the beginning of the string; otherwise, it will return None.

Let us start the coding

``````# importing the module
import re

def fun(s):

# using re.match function
a = re.match(r'[a-zA-Z0-9_-]+@[a-zA-Z0-9]+\.[a-zA-Z]{1,3}\$',s)

return(a)

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)``````

Input:

```3
lara@hackerrank.com
brian-23@hackerrank.com
britts_54@hackerrank.com```

Output:

`['brian-23@hackerrank.com', 'britts_54@hackerrank.com', 'lara@hackerrank.com']`

As you can see, we get the desired output:

## Summary

In this short tutorial, we learned about how we can validate email addresses in Python based on the conditions given in the question. We come up with three different solutions to solve the question.

## References

Related Keywords: hackerrank valid email address regex, validating email addresses with a filter hackerrank solution, valid email address hackerrank, detect the email addresses hackerrank solution, valid email address hackerrank solution in python, python functional hacker rank solution

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com