Editor

Merge Two Contact Lists

#252 · Dictionaries and sets · Beginner · 40 XP · about 8 min

You are combining the contacts from your phone and your laptop. Each list holds (name, email) tuples. The same person can turn up in both lists, or twice in one, with the email typed differently: 'sam@mail.com' and ' Sam@Mail.com' are the same address. Two emails count as the same when they are equal after removing the spaces at both ends and ignoring upper and lower case.

Write merge_contacts(phone, laptop) that returns one list of (name, email) tuples with each address only once. Go through phone first and then laptop, and keep the first contact you meet for each address, exactly as it was written, in that order.

Examples

merge_contacts(
    [('Sam', 'sam@mail.com'), ('Ivy', 'ivy@work.org')],
    [('Samuel', ' Sam@Mail.com'), ('Raj', 'raj@mail.com')],
)
# returns [('Sam', 'sam@mail.com'), ('Ivy', 'ivy@work.org'), ('Raj', 'raj@mail.com')]

Samuel's address is Sam's with a space and capital letters, so only Sam's contact is kept.

Constraints

  • Each list holds up to 5,000 contacts.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.