Velocity Reviews > RE: Question on lists

# RE: Question on lists

Dan
Guest
Posts: n/a

 07-28-2004
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=(E-Mail Removed)
[mailtoython-list-bounces+dan=(E-Mail Removed)] On Behalf Of
Kristofer Pettijohn
Sent: Tuesday, July 27, 2004 8:27 PM
To: http://www.velocityreviews.com/forums/(E-Mail Removed)
Subject: Question on 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
(E-Mail Removed)
--
http://mail.python.org/mailman/listinfo/python-list

Tony Clarke
Guest
Posts: n/a

 07-31-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" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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=(E-Mail Removed)
> [mailtoython-list-bounces+dan=(E-Mail Removed)] On Behalf Of
> Kristofer Pettijohn
> Sent: Tuesday, July 27, 2004 8:27 PM
> To: (E-Mail Removed)
> 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
> (E-Mail Removed)

Peter Otten
Guest
Posts: n/a

 07-31-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

Tony Clarke
Guest
Posts: n/a

 07-31-2004
Peter Otten <(E-Mail Removed)> wrote in message news:<cefd6i\$q21\$00\$(E-Mail Removed)-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