Re: Removing duplicates from a list

2005-09-16 Thread Steven Bethard
drochom wrote: > i suppose this one is faster (but in most cases efficiency doesn't > matter) > def stable_unique(s): > > e = {} > ret = [] > for x in s: > if not e.has_key(x): > e[x] = 1 > ret.append(x) > retur

Re: Removing duplicates from a list

2005-09-16 Thread martijn
Thanks for all the information. And now I understand the timeit module ;) GC-Martijn -- http://mail.python.org/mailman/listinfo/python-list

Re: Removing duplicates from a list

2005-09-15 Thread drochom
thanks, nice job. but this benchmark is pretty deceptive: try this: (definition of unique2 and unique3 as above) >>> import timeit >>> a = range(1000) >>> t = timeit.Timer('unique2(a)','from __main__ import unique2,a') >>> t2 = timeit.Timer('stable_unique(a)','from __main__ import stable_unique,a

Re: Removing duplicates from a list

2005-09-15 Thread 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:

Re: Removing duplicates from a list

2005-09-15 Thread drochom
i suppose this one is faster (but in most cases efficiency doesn't matter) >>> def stable_unique(s): e = {} ret = [] for x in s: if not e.has_key(x): e[x] = 1 ret.append(x) return ret cheers, przemek

Re: Removing duplicates from a list

2005-09-15 Thread drochom
there wasn't any information about ordering... maybe i'll find something better which don't destroy original ordering regards przemek -- http://mail.python.org/mailman/listinfo/python-list

Re: Removing duplicates from a list

2005-09-15 Thread 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

Re: Removing duplicates from a list

2005-09-15 Thread drochom
Rubinho napisal(a): > I've a list with duplicate members and I need to make each entry > unique. > hi, other possibility (my newest discovery:) ) >>> a = [1,2,2,4,2,1,3,4] >>> unique = d.fromkeys(a).keys() >>> unique [1, 2, 3, 4] regards przemek -- http://mail.python.org/mailman/listinfo/pyth

Re: Removing duplicates from a list

2005-09-14 Thread Steven Bethard
przemek drochomirecki wrote: > 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 comin

Re: Removing duplicates from a list

2005-09-14 Thread tcc . chapman
This works too, if speed isn't your thing.. >> a = [ 1,2,3,2,6,1,3,4,1,7,5,6,7] >> a = dict( ( (i,None) for i in a)).keys() a [1, 2, 3, 4, 5, 6, 7] -- http://mail.python.org/mailman/listinfo/python-list

Re: Removing duplicates from a list

2005-09-14 Thread 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

Re: Removing duplicates from a list

2005-09-14 Thread Will McGugan
Steven D'Aprano wrote: > > > 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

Re: Removing duplicates from a list

2005-09-14 Thread Steven D'Aprano
On Wed, 14 Sep 2005 13:28:58 +0100, Will McGugan wrote: > Rubinho wrote: >> 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. Don't

Re: Removing duplicates from a list

2005-09-14 Thread Rocco Moretti
Rubinho wrote: > 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 lengt

Re: Removing duplicates from a list

2005-09-14 Thread 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. -- http://mail.python.org/mailman/listinfo/python-list

Re: Removing duplicates from a list

2005-09-14 Thread Christian Stapfer
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >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

Re: Removing duplicates from a list

2005-09-14 Thread Rubinho
Peter Otten wrote: > Rubinho wrote: > > > 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

Re: Removing duplicates from a list

2005-09-14 Thread Peter Otten
Rubinho wrote: > 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: >

Re: Removing duplicates from a list

2005-09-14 Thread Will McGugan
Rubinho wrote: > 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: >

Re: Removing duplicates from a list

2005-09-14 Thread 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) > my

Removing duplicates from a list

2005-09-14 Thread 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: