3 ms·
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 acrobatic
by plg 8y 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>))
- occams_chainsaw 8y agoEasier: no_duplicates = set(<list>)
- dagw 8y agoExcept 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>))