Clean the Contact List

Python for AIHard+10 XP+10

Clean the Contact List

A contact list was exported from an old app. Each line should hold a name and an email, separated by a comma, but everything is messy.

Task: write clean_contacts(lines) that returns a list of tidy contacts, each in the form "Name <email>".

For each line:

  • Split it at the first comma only. The part before is the name, the part after is the email.
  • Name: remove whitespace at the ends, use exactly one space between words, and capitalise the first letter of each word (the rest lowercase).
  • Email: remove whitespace at the ends and make it all lowercase.
  • Skip any line that has no comma at all.
  • Skip any line where the name or the email is empty after cleaning.

Keep the contacts in their original order. An empty list gives [].

Examples

lines = [" ADA lovelace , ADA@Example.COM ", " ", "grace hopper,grace@navy.mil"]
returns ['Ada Lovelace <ada@example.com>', 'Grace Hopper <grace@navy.mil>']
lines = []
returns []

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run