Hacker News new | ask | show | jobs
by plg 2695 days ago
So to get a list of unique values you suggest I convert to a dictionary and then convert back to a list? Is this the Python way? I dunno it seems like acrobatics

  no_duplicates = list(dict.fromkeys(<list>))
1 comments

Easier:

  no_duplicates = set(<list>)
Except that won't return a list. You need list(set(<list>)). Also using set means you lose list ordering, which isn't the case when using dict (assuming python 3.6 or higher).

That being said, and not that it should ever make a difference, list(set(<list>)) is 2-3 times faster than list(dict.fromkeys(<list>))