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

Tarea

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.

Solución

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 3
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

close

bookChallenge: Email List Cleaner

Desliza para mostrar el menú

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.

Tarea

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.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 3
single

single

some-alt