Is there a faster way to do this in python?
[f for f in list_1 if not f in list_2]
list_1 and list_2 both consist of about 120.000 strings. It takes about 4 minutes to generate the new list.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you put
list_2into aset, it should make the containment checking a lot quicker:This is because
x in listis an O(n) check, whilex in setis constant-time.Another way is to use set-difference:
However, this probably won’t be faster than the first way – also, it’ll eliminate duplicates from
list_1which you may not want.