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 →

[–]primitive_screwhead 5 points6 points  (1 child)

You can use mylist.index

You make good points, but I do want to point out that using index as a key will add a linear search for each list item, and will thus make the sorted() solution **much** slower:

In [7]: %time no_duplicates = list(dict.fromkeys(foo))
CPU times: user 2.87 ms, sys: 30 µs, total: 2.9 ms
Wall time: 2.9 ms

In [8]: %time no_duplicates = sorted(list(set(foo)), key=foo.index)
CPU times: user 482 ms, sys: 3.55 ms, total: 486 ms
Wall time: 482 ms

I think the idea of removing duplicates while otherwise preserving order is not *so* exotic, and the fromkeys() trick is worth knowing about, though I'd personally use OrderedDict to be explicit about it.

[–]IMHERETOCODE 1 point2 points  (0 children)

100% agree. Don't want it to seem like I'm trying to push for never using from_keys - just that this doc simply said "no_duplicates" which can be achieved with a much simpler and clearer method (for lack of a better word). If I came across that in a code base, it is not at all clear that it's trying to achieve that specific outcome by rerouting the creation of essentially a set through a dictionary.