python - How do you remove duplicates from a list in whilst preserving order? -


Is there a built-in that removes duplicates from the list in Python, while preserving the order? I know that I can use a set to remove duplicates, but it destroys the original order. I also know that I can do my roll in this way:

  def uniq (input): output = [] for input x: if x is not in output: output.append (X) Return output  

(Thanks for this.)

But if I could, I would like to take advantage of an underlying or more Pythonic idiom itself.

Related questions:

Here are some options:

Fastest one:

  def f7 (seq): seen = set () seen_add = seen.add return [for xx in seq if not (seen in x or seen_add (x) )]]  

Why seen.add was specified instead of seen_add instead of viewed .add Please, specify? Python is a dynamic language, and is viewed. By solving , each move is more expensive than solving a local variable. can be seen.add between iterations, and the runtime is not smart enough to rule over it. To play it safe, the object has to be checked every time.

If you plan to use this function on the same dataset, then maybe you would be better than an ordered set:

o (1) Convergence, Deletion and Member-check per operation.


Comments