"Collapsing" a list into a list of changes

Tony aclarke11 at yahoo.co.uk
Sat Feb 5 14:48:43 EST 2005


Alan McIntyre wrote:
> Tony,
>
> Actually I only want to remove a certain kind of duplication; <snip>

How about this one liner?
def condense(m):
                    print [m[0]]+[m[k] for k in range(1,len(m)) if
m[k]!=m[k-1]]
b=[1,1,1,2,2,2,1,1,1]
condense(b)
Tony Clarke




More information about the Python-list mailing list