Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Challenge: Email List Cleaner | Automating Growth Tasks
Python for Growth Hackers

bookChallenge: Email List Cleaner

When preparing for a growth campaign, you often need to work with a large set of email addresses. However, these lists can be messy—filled with duplicates, extra spaces, and invalid entries. Cleaning your email list not only improves deliverability but also ensures you are reaching real people, maximizing the impact of your outreach. Automating this process with Python saves you time and reduces the risk of human error.

To tackle this challenge, you will write a Python function that automates the cleaning of an email list. Your function should perform several steps: remove duplicate addresses, trim unnecessary whitespace from each email, and filter out any strings that do not contain the @ symbol, which is essential for a valid email format. By following these steps, you will prepare a high-quality list that is ready for your next campaign.

123456789101112131415161718192021222324252627282930313233343536
def clean_email_list(): # Hardcoded list of emails with duplicates, whitespace, and invalid entries raw_emails = [ "alice@example.com", " bob@example.com ", "charlie@example.com", "alice@example.com", # duplicate "daveexample.com", # missing '@' "eve@sample.com", "frank@sample.com ", " grace@sample.com", "bob@example.com", # duplicate "invalidemail.com", # missing '@' "HEIDI@SAMPLE.COM", "heidi@sample.com", # different case, but valid "ivan@example.com" ] # Step 1: Trim whitespace and convert all emails to a standard case (optional) trimmed_emails = [email.strip() for email in raw_emails] # Step 2: Filter out emails that do not contain '@' valid_emails = [email for email in trimmed_emails if "@" in email] # Step 3: Remove duplicates while preserving order seen = set() cleaned_list = [] for email in valid_emails: if email not in seen: cleaned_list.append(email) seen.add(email) return cleaned_list # Output the cleaned email list print(clean_email_list())
copy

This function demonstrates a clear workflow for cleaning your email list. It starts with a raw list that includes duplicates, extra spaces, and invalid entries. By trimming whitespace, filtering for the "@" symbol, and removing duplicates, you end up with a list of unique, valid emails ready for outreach. Notice how preserving the original order can be important if you want to maintain the sequence of your contacts.

Taak

Swipe to start coding

Write your own function called prepare_email_list that:

  • Uses a hardcoded list of email addresses with duplicates, whitespace, and some invalid entries.
  • Removes duplicate emails (keep only the first occurrence).
  • Trims whitespace from each email address.
  • Filters out emails that do not contain the "@" symbol.
  • Returns the cleaned list.

Test your function by printing the returned list.

Oplossing

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 3
single

single

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you explain how the function preserves the original order of emails?

What if I want to make the email comparison case-insensitive when removing duplicates?

How can I modify the function to accept a list of emails as an argument instead of using a hardcoded list?

close

bookChallenge: Email List Cleaner

Veeg om het menu te tonen

When preparing for a growth campaign, you often need to work with a large set of email addresses. However, these lists can be messy—filled with duplicates, extra spaces, and invalid entries. Cleaning your email list not only improves deliverability but also ensures you are reaching real people, maximizing the impact of your outreach. Automating this process with Python saves you time and reduces the risk of human error.

To tackle this challenge, you will write a Python function that automates the cleaning of an email list. Your function should perform several steps: remove duplicate addresses, trim unnecessary whitespace from each email, and filter out any strings that do not contain the @ symbol, which is essential for a valid email format. By following these steps, you will prepare a high-quality list that is ready for your next campaign.

123456789101112131415161718192021222324252627282930313233343536
def clean_email_list(): # Hardcoded list of emails with duplicates, whitespace, and invalid entries raw_emails = [ "alice@example.com", " bob@example.com ", "charlie@example.com", "alice@example.com", # duplicate "daveexample.com", # missing '@' "eve@sample.com", "frank@sample.com ", " grace@sample.com", "bob@example.com", # duplicate "invalidemail.com", # missing '@' "HEIDI@SAMPLE.COM", "heidi@sample.com", # different case, but valid "ivan@example.com" ] # Step 1: Trim whitespace and convert all emails to a standard case (optional) trimmed_emails = [email.strip() for email in raw_emails] # Step 2: Filter out emails that do not contain '@' valid_emails = [email for email in trimmed_emails if "@" in email] # Step 3: Remove duplicates while preserving order seen = set() cleaned_list = [] for email in valid_emails: if email not in seen: cleaned_list.append(email) seen.add(email) return cleaned_list # Output the cleaned email list print(clean_email_list())
copy

This function demonstrates a clear workflow for cleaning your email list. It starts with a raw list that includes duplicates, extra spaces, and invalid entries. By trimming whitespace, filtering for the "@" symbol, and removing duplicates, you end up with a list of unique, valid emails ready for outreach. Notice how preserving the original order can be important if you want to maintain the sequence of your contacts.

Taak

Swipe to start coding

Write your own function called prepare_email_list that:

  • Uses a hardcoded list of email addresses with duplicates, whitespace, and some invalid entries.
  • Removes duplicate emails (keep only the first occurrence).
  • Trims whitespace from each email address.
  • Filters out emails that do not contain the "@" symbol.
  • Returns the cleaned list.

Test your function by printing the returned list.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 3
single

single

some-alt