Question on lists

Peter Otten __peter__ at web.de
Sat Jul 31 08:11:30 CEST 2004


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]]
> 
> l=[1,2,2,4,3,5,5,6]
> condense(l)
> gives:
>    >>> 
> [1, 2, 4, 3, 5, 6]
>>>> 

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





More information about the Python-list mailing list