python - An interesting code for obtaining unique values from a list -



python - An interesting code for obtaining unique values from a list -

say given list s = [2,2,2,3,3,3,4,4,4]

i saw next code beingness used obtain unique values s:

unique_s = sorted(unique(s))

where unique defined as:

def unique(seq): # not order preserving set = {} map(set.__setitem__, seq, []) homecoming set.keys()

i'm curious know if there difference between , doing list(set(s))? both results in mutable object same values.

i'm guessing code faster since it's looping 1 time rather twice in case of type conversion?

you should utilize code describe:

list(set(s))

this works on pythons 2.4 (i think) 3.3, concise, , uses built-ins in easy understand way.

the function unique appears designed work if set not built-in, true python 2.3. python 2.3 ancient (2003). unique function broken python 3.x series, since dict.keys returns iterator python 3.x.

python

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -