Data science: Remove duplicate records

A mailing list has the same people more than once, with emails typed differently. Keep one record per email.

What to build

A program that removes duplicate records by a normalised key.

Requirements

  • The key of a record is its email with surrounding spaces removed and in lower case. Keep the first record for each key.
  • Print kept: K and removed: R, then each kept record as EMAIL,NAME with the normalised email and the name without surrounding spaces, in input order.

Input

Line 1: n. Then n lines email,name (the email has no commas).

Output

Two lines, then the kept records.

Constraints

1 ≤ n ≤ 1000.

Example

Input:

4
Asha@Mail.com,Asha
ben@mail.com,Ben
 asha@mail.com ,Asha K
BEN@MAIL.COM,Benjamin

Output:

kept: 2
removed: 2
asha@mail.com,Asha
ben@mail.com,Ben

Why it works

Normalising the key first means that "Asha@Mail.com" and " asha@mail.com " are recognised as the same person, which is the step that comparing raw text would miss.

Examples

Example 1 · Input
4
Asha@Mail.com,Asha
ben@mail.com,Ben
 asha@mail.com ,Asha K
BEN@MAIL.COM,Benjamin
Expected output
kept: 2
removed: 2
asha@mail.com,Asha
ben@mail.com,Ben
Example 2 · Input
2
a@x.org,A
b@x.org,B
Expected output
kept: 2
removed: 0
a@x.org,A
b@x.org,B

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Normalise the email (strip and lower case) before checking whether it has been seen.

Hint 2

A set of seen emails and a list of kept rows are all you need.

Solution

Show one correct solution (try it yourself first)
n = int(input())
seen = set()
kept = []
for _ in range(n):
    email, name = input().split(",", 1)
    key = email.strip().lower()
    if key in seen:
        continue
    seen.add(key)
    kept.append((key, name.strip()))
print("kept:", len(kept))
print("removed:", n - len(kept))
for email, name in kept:
    print(email + "," + name)

Lesson: Cleaning data →