# Question on lists

Tony Clarke a.clarke11 at ntlworld.com
Sat Jul 31 14:59:03 CEST 2004

```Peter Otten <__peter__ at web.de> wrote in message news:<cefd6i\$q21\$00\$1 at news.t-online.com>...
> Tony Clarke wrote:
>
> > A simple list comprehension approach:
> >
> > def condense(m):
> > print [m[k] for k in range(len(m)) if m[k]!=m[k-1]]
> >
> >Snip
>
> That does not work for len(m) == 1 and for m[0] == m[-1]:
>
> >>> def condense(m):
> ...     print [m[k] for k in range(len(m)) if m[k]!=m[k-1]]
> ...
> >>> condense([1,2,1]) # expected: [1,2,1]
>  [2, 1]
> >>> condense([42]) # expected: [42]
> []
>
> Peter

Oops! more testing was indicated!
How's this:

def condense(m):
print m, 'condensed is',
print [m[0]]+[m[k] for k in range(1,len(m)) if m[k]!=m[k-1]]

l=[1,2,2,4,3,5,5,6]
condense(l)
n=[1,2,1]
m=[42]
o=[0,0,1,2,3,3,0,0]
condense (n)
condense (m)
condense(o)

>>>
[1, 2, 2, 4, 3, 5, 5, 6] condensed is [1, 2, 4, 3, 5, 6]
[1, 2, 1] condensed is [1, 2, 1]
[42] condensed is [42]
[0, 0, 1, 2, 3, 3, 0, 0] condensed is [0, 1, 2, 3, 0]
>>>
Cheers
Tony Clarke

```