Matheus Tenório
Matheus Tenório

Reputation: 47

How to match two Python lists and find missing values

I'm creating a function that syncs my db and an external email service. I wanted to get all users registered in this email service and match them with my db.

Is there a way in Python for me to take two lists (one from my db and one from email service) and match if the db list has any email that is not in the email service's list, so I can use it to register them?

Upvotes: 1

Views: 550

Answers (1)

gregory
gregory

Reputation: 12925

Convert the lists into sets and use a symmetric difference:

list(set(list1) ^ set(list2)) 

This will give you a list of differences between both lists. If you really only want what is missing from your email list, then subtract your database list from your email list:

list(set(email_list) - set(database_list)) 

This will give you a list of emails not already registered.

Upvotes: 1

Related Questions