470,821 Members | 1,972 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,821 developers. It's quick & easy.

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
kr*******@cybernetik.net
Jul 18 '05 #1
10 1277
On Wed, Jul 28, 2004 at 03:26:54AM +0000, Kristofer Pettijohn wrote:
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 ;)


Nothing builtin that I know of, but it's trivial to write:

def uniq(iterable):
"""Remove consecutive duplicates from a sequence."""

prev = object()
for element in iterable:
if element != prev:
prev = element
yield element
list(uniq(['a', 'b', 'c', 'c', 'd', 'd', 'd', 'e'])) ['a', 'b', 'c', 'd', 'e'] list(uniq(['a', 'b', 'c', 'c', 'd', 'c', 'd', 'e']))

['a', 'b', 'c', 'd', 'c', 'd', 'e']

-Andrew.

Jul 18 '05 #2
Am Mittwoch, 28. Juli 2004 05:26 schrieb Kristofer Pettijohn:
My question is about lists:

Is there a way to remove duplicate items from a list that are
next to each other?


They don't even have to be next to each other:

(>= Python 2.3 required)
import sets
list(sets.Set(['a','a','b','b','d','b','c','a','ladida']))

['a', 'c', 'b', 'd', 'ladida']

This doesn't maintain the order of the items, though. If you need that, and
you're sure your list is always sorted, use the example Andrew gave.

Heiko.
Jul 18 '05 #3
Andrew Bennetts <an***************@puzzling.org> wrote:
Nothing builtin that I know of, but it's trivial to write:

def uniq(iterable):
"""Remove consecutive duplicates from a sequence."""

prev = object()
for element in iterable:
if element != prev:
prev = element
yield element


Thank you! Easier than I thought. I don't know why I make things
so hard on myself.

--
Kristofer Pettijohn
kr*******@cybernetik.net
Jul 18 '05 #4
Heiko Wundram <he*****@ceosg.de> wrote:
import sets
list(sets.Set(['a','a','b','b','d','b','c','a','ladida']))

['a', 'c', 'b', 'd', 'ladida']

This doesn't maintain the order of the items, though. If you need that, and
you're sure your list is always sorted, use the example Andrew gave.


Good to know for the future; unfortunately these needed to remain
in order - I'm graphing routes and paths, and if the same node shows
up 2+ times in a row, I needed them weeded out.

Thanks!

--
Kristofer Pettijohn
kr*******@cybernetik.net
Jul 18 '05 #5
Andrew Bennetts wrote:
Nothing builtin that I know of, but it's trivial to write:


Using 2.4, looks like the logic is in itertools.groupby:
from itertools import groupby
l = ['a', 'b', 'c', 'c', 'd', 'd', 'd', 'e']
list(x for x, _ in itertools.groupby(l))

['a', 'b', 'c', 'd', 'e']

--
Ciao,
Matteo
Jul 18 '05 #6
If x is the given sequence, then
[x[i] for i in filter(lambda i: i==0 or x[i-1]<>x[i], range(len(x)))]
is what you want.

"Kristofer Pettijohn" <kr*******@cybernetik.net> wrote in message
news:41***********************@newsreader.cybernet ik.net...
| 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
| kr*******@cybernetik.net
Jul 18 '05 #7
In article <VaKNc.126557$od7.8959@pd7tw3no>,
Elaine Jackson <el***************@home.com> wrote:

If x is the given sequence, then
[x[i] for i in filter(lambda i: i==0 or x[i-1]<>x[i], range(len(x)))]
is what you want.


Please report to the UnPythonic Activities Committee. ;-)
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"To me vi is Zen. To use vi is to practice zen. Every command is a
koan. Profound to the user, unintelligible to the uninitiated. You
discover truth everytime you use it." --*****@lion.austin.ibm.com
Jul 18 '05 #8
"Heiko Wundram" <he*****@ceosg.de> wrote in message
news:ma*************************************@pytho n.org...
import sets
list(sets.Set(['a','a','b','b','d','b','c','a','ladida']))

['a', 'c', 'b', 'd', 'ladida']

Newbie question:
OK, this is great. But I was not aware of this module,
and I was working in my own sets.py.
So how do I handle this namespace issue?
I.e., how can I 'import sets' and get the Python module,
when my own script is named sets.py?

Thanks,
Alan Isaac

Jul 18 '05 #9
On Thu, 29 Jul 2004, Alan G Isaac wrote:
Newbie question:
OK, this is great. But I was not aware of this module,
and I was working in my own sets.py.
So how do I handle this namespace issue?
I.e., how can I 'import sets' and get the Python module,
when my own script is named sets.py?


The best way, of course, is to rename your script ;) it's never a good
idea to give a script the same name as that of a module you plan to use.

If you absolutely must leave your script named sets.py, you can use the
following trick to get to Python's sets.py:

import sys
sys.path.remove('')
import sets

This removes the current directory from Python's search path. Note you
won't be able to access any modules in the current directory after that
line, unless you do a "sys.path.insert(0,'')" first.

Jul 18 '05 #10
Aahz <aa**@pythoncraft.com> wrote:
Elaine Jackson <el***************@home.com> wrote:

If x is the given sequence, then
[x[i] for i in filter(lambda i: i==0 or x[i-1]<>x[i], range(len(x)))]
is what you want.
Please report to the UnPythonic Activities Committee. ;-)
Has the committee considered the other functional solution?
def dedupe(lst, x): .... if not lst or lst[-1] != x:
.... lst.append(x)
.... return lst
.... reduce(dedupe, l, []) ['a', 'b', 'c', 'd', 'e']

If it would help, I'm sure we could get an obfuscation expert to make
it into one line, but without a first-class conditional expression,
it's nothing but a circus trick. Perl envy, like.

Oh, okay, twist my arm...
reduce(lambda r,x: (not r or r[-1]!=x) and r.append(x) or r, l, [])

['a', 'b', 'c', 'd', 'e']

I knew reduce would come in handy for something.
"To me vi is Zen. To use vi is to practice zen. Every command is a
koan. Profound to the user, unintelligible to the uninitiated. You
discover truth everytime you use it." --*****@lion.austin.ibm.com


Apparently vi also shares with koans the characteristic of battering
down the walls of rationality. :-)

--
Although we may never know with complete certainty the identity
of the winner of this year's presidential election, the identity
of the loser is perfectly clear. It is the nation's confidence
in the judge as an impartial guardian of the law.
- Justice John Paul Stevens, from his dissenting opinion Dec 12, 2000
Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sean Berry | last post: by
9 posts views Thread by Dave H | last post: by
19 posts views Thread by CMAR | last post: by
22 posts views Thread by Nunaya | last post: by
5 posts views Thread by Roger Bonine | last post: by
10 posts views Thread by JustSomeGuy | last post: by
5 posts views Thread by bruce | last post: by
90 posts views Thread by John Salerno | last post: by
5 posts views Thread by Scott | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.