Question on lists

Tony Clarke a.clarke11 at ntlworld.com
Sat Jul 31 02:49:06 CEST 2004


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]
>>> 

Cheers
Tony Clarke

"Dan" <dan at eloff.info> wrote in message news:<mailman.982.1091210395.5135.python-list at python.org>...
> I've got a SortedList class I wrote with a unique() member function that
> does just this. I'm going to post it to the Python cookbook soon. If
> you're interested I can send along a copy.
> 
> -Dan
> 
> -----Original Message-----
> From: python-list-bounces+dan=eloff.info at python.org
> [mailto:python-list-bounces+dan=eloff.info at python.org] On Behalf Of
> Kristofer Pettijohn
> Sent: Tuesday, July 27, 2004 8:27 PM
> To: python-list at python.org
> Subject: Question on lists
> 
> My question is about lists:
> 
> Is there a way to remove duplicate items from a list that are
> next to each other?
> 
> Example...
> 
> Performing the operation on ['a', 'b', 'c', 'c', 'd', 'd', 'd', 'e']
> will return ['a', 'b', 'c', 'd', 'e']
> 
> Performing the operation on ['a', 'b', 'c', 'c', 'd', 'c', 'd', 'e']
> will return ['a', 'b', 'c', 'd', 'c', 'd', 'e']
> 
> I'm guessing there is probably nothing internal that will do it, so
> I may have to write something on my own - just thought I'd check
> first ;)
> 
> Thanks!
> 
> -- 
> Kristofer Pettijohn
> kristofer at cybernetik.net



More information about the Python-list mailing list