On Jun 3, 1:22Â am, Peter Otten <[EMAIL PROTECTED]> wrote: > > Yes :) > > Seriously, you are using O(n) containers and O(n) lookup where mine uses > O(1). For short lists it doesn't matter, but as the list length grows the > difference gets huge: > > $ cat unique.py > def unique(items): > Â Â u = set(items) > Â Â if len(u) == len(items): > Â Â Â Â return items > Â Â result = [] > Â Â for item in items: > Â Â Â Â if item in u: > Â Â Â Â Â Â result.append(item) > Â Â Â Â Â Â u.remove(item) > Â Â return result >
Yikes... and the list comprehension looked so innocent. I had resigned myself to O(n) lookup, but you and Raymond appear to agree on the basic concept for unique() -- check set membership, then generate the final sequence and update the set based on that. -- http://mail.python.org/mailman/listinfo/python-list