Python list(set(list(...)) to remove duplicates -
is
list(set(some_list)) a way remove duplicates list? (python 3.3 if matters)
(edited address of comments... perhaps terse before).
specifically,
- is @ least comparable, in terms of efficiency (mainly speed memory), if not better writing ones own algorithm; it's concise code
- is reliable? situations breaks? (one has been mentioned ... list items need hashable)
- is there more pythonesque way of doing it?
the method show shortest , easiest understand; make pythonic definitions.
if need preserve order of list, can use collections.ordereddict instead of set:
list(collections.ordereddict((k, none) k in some_list).keys()) if elements aren't hashable can sorted, can use itertools.groupby remove duplicates:
list(k k,g in itertools.groupby(sorted(some_list)))
Comments
Post a Comment