Challenge: 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.
123456789101112131415161718192021222324252627282930313233343536def 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())
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.
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.
Solution
Merci pour vos commentaires !
single
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
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?
Génial!
Completion taux amélioré à 5
Challenge: Email List Cleaner
Glissez pour afficher le 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.
123456789101112131415161718192021222324252627282930313233343536def 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())
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.
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.
Solution
Merci pour vos commentaires !
single