How do I remove duplicates from a list, while preserving order? Using a set to remove duplicates destroys the original order. Is there a built-in or a Pythonic idiom?
Share
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.
Here you have some alternatives: http://www.peterbe.com/plog/uniqifiers-benchmark
Fastest one:
Why assign
seen.addtoseen_addinstead of just callingseen.add? Python is a dynamic language, and resolvingseen.addeach iteration is more costly than resolving a local variable.seen.addcould have changed between iterations, and the runtime isn’t smart enough to rule that out. To play it safe, it has to check the object each time.If you plan on using this function a lot on the same dataset, perhaps you would be better off with an ordered set: http://code.activestate.com/recipes/528878/
O(1) insertion, deletion and member-check per operation.
(Small additional note:
seen.add()always returnsNone, so theorabove is there only as a way to attempt a set update, and not as an integral part of the logical test.)