Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda 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.

Tarefa

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.

Solução

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

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

Deslize para mostrar o menu

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.

Tarefa

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.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3
single

single

some-alt