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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -