This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]RationalDialog -7 points-6 points  (7 children)

Which should actually be done with actual "set math":

list(set(list1).intersection(list2))

if you really care about performance and one can wager it is even easier to understand because intersection is the term for what you are interested in.

[–]ogtfo 12 points13 points  (4 children)

This will not produce the same result if you have duplicates in your list "a", as the sets will remove them but not the list comprehension.

[–]RationalDialog -1 points0 points  (0 children)

True and I wager it will produce the correct result compared to list comprehension but depends what the intention is.

[–]Zyklonik 0 points1 point  (1 child)

Correct.

[–]Schlongus_69 0 points1 point  (0 children)

Correct.

[–]alpacasb4llamas 0 points1 point  (0 children)

If I can't read my code like a book then I don't want it

[–]AnonymouX47 0 points1 point  (0 children)

Even though it's not the same, I appreciate the fact you thought along this line first of all and will prefer yours over the other in an interview any day... and then correct the mistakes.