python - Loopless program to remove duplicate elements in a sorted array -


i want write loopless program (probably using comprehension) remove duplicate elements in sorted array in python (and efficiently too).

since list sorted - meaning duplicates grouped, can use itertools.groupby

>>> testlist = [1, 1, 1, 2, 3, 3, 4, 4, 5, 6, 7, 8, 8, 9] >>> itertools import groupby >>> [k k, g in groupby(testlist)] [1, 2, 3, 4, 5, 6, 7, 8, 9] 

this more efficient (in memory , time) converting set , sorting. has advantage of needing compare equality, works ok unhashable items too.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -