Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Email List Cleaner | Automating Growth Tasks
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
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.

Uppgift

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.

Lösning

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 3
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

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

Svep för att visa menyn

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.

Uppgift

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.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 3
single

single

some-alt