Removing duplicates from a list

R

Rubinho

I've a list with duplicate members and I need to make each entry
unique.

I've come up with two ways of doing it and I'd like some input on what
would be considered more pythonic (or at least best practice).

Method 1 (the traditional approach)

for x in mylist:
if mylist.count(x) > 1:
mylist.remove(x)

Method 2 (not so traditional)

mylist = set(mylist)
mylist = list(mylist)

Converting to a set drops all the duplicates and converting back to a
list, well, gets it back to a list which is what I want.

I can't imagine one being much faster than the other except in the case
of a huge list and mine's going to typically have less than 1000
elements.

What do you think?

Cheers,

Robin
 
T

Thomas Guettler

Am Wed, 14 Sep 2005 04:38:35 -0700 schrieb Rubinho:
I've a list with duplicate members and I need to make each entry
unique.

I've come up with two ways of doing it and I'd like some input on what
would be considered more pythonic (or at least best practice).
mylist = set(mylist)
mylist = list(mylist)

Converting to a set drops all the duplicates and converting back to a
list, well, gets it back to a list which is what I want.

I can't imagine one being much faster than the other except in the case
of a huge list and mine's going to typically have less than 1000
elements.

What do you think?

Hi,

I would use "set":

mylist=list(set(mylist))

Thomas
 
W

Will McGugan

Rubinho said:
I've a list with duplicate members and I need to make each entry
unique.

I've come up with two ways of doing it and I'd like some input on what
would be considered more pythonic (or at least best practice).

Method 1 (the traditional approach)

for x in mylist:
if mylist.count(x) > 1:
mylist.remove(x)

Method 2 (not so traditional)

mylist = set(mylist)
mylist = list(mylist)

Converting to a set drops all the duplicates and converting back to a
list, well, gets it back to a list which is what I want.

I can't imagine one being much faster than the other except in the case
of a huge list and mine's going to typically have less than 1000
elements.

I would imagine that 2 would be significantly faster. Method 1 uses
'count' which must make a pass through every element of the list, which
would be slower than the efficient hashing that set does. I'm also not
sure about removing an element whilst iterating, I think thats a no-no.

Will McGugan
 
P

Peter Otten

Rubinho said:
I've a list with duplicate members and I need to make each entry
unique.

I've come up with two ways of doing it and I'd like some input on what
would be considered more pythonic (or at least best practice).

Method 1 (the traditional approach)

for x in mylist:
if mylist.count(x) > 1:
mylist.remove(x)

That would be an odd tradition:
mylist = [1, 2, 1, 3, 2, 3]
for x in mylist:
.... if mylist.count(x) > 1:
.... mylist.remove(x)
....[2, 1, 2, 3] # oops!

See "Unexpected Behavior Iterating over a Mutating Object"
http://mail.python.org/pipermail/python-list/2005-September/298993.html
thread for the most recent explanation.

Rather, the traditional approach for an algorithmic problem in Python is to
ask Tim Peters, see his recipe at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560/
(which predates Python's set class).

Peter
 
R

Rubinho

Peter said:
That would be an odd tradition:

By tradition I wasn't really talking Python tradition; what I meant was
that the above pattern is similar to what would be generated by people
used to traditional programming languages.
mylist = [1, 2, 1, 3, 2, 3]
for x in mylist:
... if mylist.count(x) > 1:
... mylist.remove(x)
...[2, 1, 2, 3] # oops!

But you're absolutely right, it doesn't work! Oops indeed :)

I've gone with Thomas's suggestion above of: mylist=list(set(mylist))

Thanks,

Robin
 
M

martijn

I do this:

def unique(keys):
unique = []
for i in keys:
if i not in unique:unique.append(i)
return unique

I don't know what is faster at the moment.
 
C

Christian Stapfer

I do this:

def unique(keys):
unique = []
for i in keys:
if i not in unique:unique.append(i)
return unique

I don't know what is faster at the moment.

This is quadratic, O(n^2), in the length n of the list
if all keys are unique.
Conversion to a set just might use a better sorting
algorithm than this (i.e. n*log(n)) and throwing out
duplicates (which, after sorting, are positioned
next to each other) is O(n). If conversion
to a set should turn out to be slower than O(n*log(n))
[depending on the implementation], then you are well
advised to sort the list first (n*log(n)) and then
throw out the duplicate keys with a single walk over
the list. In this case you know at least what to
expect for large n...

Regards,
Christian
 
R

Rocco Moretti

Rubinho said:
I can't imagine one being much faster than the other except in the case
of a huge list and mine's going to typically have less than 1000
elements.

To add to what others said, I'd imagine that the technique that's going
to be fastest is going to depend not only on the length of the list, but
also the estimated redundancy. (i.e. a technique that gives good
performance with a list that has only one or two elements duplicated
might be painfully slow when there is 10-100 copies of each element.)

There really is no substitute for profiling with representitive data sets.
 
S

Steven D'Aprano

I would imagine that 2 would be significantly faster.

Don't imagine, measure.

Resist the temptation to guess. Write some test functions and time the two
different methods. But first test that the functions do what you expect:
there is no point having a blindingly fast bug.

Method 1 uses
'count' which must make a pass through every element of the list, which
would be slower than the efficient hashing that set does.

But count passes through the list in C and is also very fast. Is that
faster or slower than the hashing code used by sets? I don't know, and
I'll bet you don't either.
 
W

Will McGugan

Steven said:
Don't imagine, measure.

Resist the temptation to guess. Write some test functions and time the two
different methods. But first test that the functions do what you expect:
there is no point having a blindingly fast bug.

Thats is absolutely correct. Although I think you do sometimes have to
guess. Otherwise you would write multiple versions of every line of code.
But count passes through the list in C and is also very fast. Is that
faster or slower than the hashing code used by sets? I don't know, and
I'll bet you don't either.


Sure. But if I'm not currently optimizing I would go for the method with
the best behaviour, which usualy means hashing rather than searching.
Since even if it is actualy slower - its not likely to be _very_ slow.


Will McGugan
 
P

przemek drochomirecki

I've a list with duplicate members and I need to make each entry
unique.

I've come up with two ways of doing it and I'd like some input on what
would be considered more pythonic (or at least best practice).

Method 1 (the traditional approach)

for x in mylist:
if mylist.count(x) > 1:
mylist.remove(x)

Method 2 (not so traditional)

mylist = set(mylist)
mylist = list(mylist)

Converting to a set drops all the duplicates and converting back to a
list, well, gets it back to a list which is what I want.

I can't imagine one being much faster than the other except in the case
of a huge list and mine's going to typically have less than 1000
elements.

What do you think?

Cheers,

Robin

Hi,

Try this:

def unique(s):
e = {}
for x in s:
if not e.has_key(x):
e[x] = 1
return e.keys()

Regards
Przemek
 
S

Steven Bethard

przemek said:
def unique(s):
e = {}
for x in s:
if not e.has_key(x):
e[x] = 1
return e.keys()

This is basically identical in functionality to the code:

def unique(s):
return list(set(s))

And with the new-and-improved C implementation of sets coming in Python
2.5, there's even more of a reason to use them when you can.

STeVe
 
M

martijn

Look at the code below

def unique(s):
return list(set(s))

def unique2(keys):
unique = []
for i in keys:
if i not in unique:unique.append(i)
return unique

tmp = [0,1,2,4,2,2,3,4,1,3,2]
print tmp
print unique(tmp)
print unique2(tmp)
--------------------------
[0, 1, 2, 4, 2, 2, 3, 4, 1, 3, 2]
[0, 1, 2, 3, 4]
[0, 1, 2, 4, 3]

As you can see the end result is not the same.
I must get the end result [0, 1, 2, 4, 3] and not [0, 1, 2, 3, 4].
Thats why I use unique2()
 
D

drochom

there wasn't any information about ordering...
maybe i'll find something better which don't destroy original ordering

regards
przemek
 
D

drochom

i suppose this one is faster (but in most cases efficiency doesn't
matter)
e = {}
ret = []
for x in s:
if not e.has_key(x):
e[x] = 1
ret.append(x)
return ret

cheers,
przemek
 
M

martijn

Ow thanks , i'm I newbie and I did this test. (don't know if this is
the best way to do a small speed test)

import timeit

def unique2(keys):
unique = []
for i in keys:
if i not in unique:unique.append(i)
return unique

def unique3(s):
e = {}
ret = []
for x in s:
if not e.has_key(x):
e[x] = 1
ret.append(x)
return ret

tmp = [0,1,2,4,2,2,3,4,1,3,2]
s = """\
try:
str.__nonzero__
except AttributeError:
pass
"""
t = timeit.Timer(stmt=s)
print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
print tmp
print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
print unique2(tmp)
print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
print unique3(tmp)
print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
---------------------
5.80 usec/pass
[0, 1, 2, 4, 2, 2, 3, 4, 1, 3, 2]
7.51 usec/pass
[0, 1, 2, 4, 3]
6.93 usec/pass
[0, 1, 2, 4, 3]
6.45 usec/pass <--- your code unique2(s):
 
D

drochom

thanks, nice job. but this benchmark is pretty deceptive:

try this:
(definition of unique2 and unique3 as above)
51.52945844819817

unique2 has quadratic complexity
unique3 has amortized linear complexity
what it means?
it means that speed of your algorithm strongly depends on
len(unique2(a)). the greater distinct elements in a the greater
difference in execution time of both implementations

regards
przemek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top