Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8028

Re: Is there any advantage or disadvantage to using sets over list comps to ensure a list of unique entries?

References <5b73ae60-506f-45e4-a82c-e59571252d47@w4g2000yqm.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-06-20 13:59 -0600
Subject Re: Is there any advantage or disadvantage to using sets over list comps to ensure a list of unique entries?
Newsgroups comp.lang.python
Message-ID <mailman.191.1308600028.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Jun 20, 2011 at 1:43 PM, deathweaselx86 <deathweasel@gmail.com> wrote:
> Howdy guys, I am new.
>
> I've been converting lists to sets, then back to lists again to get
> unique lists.
> e.g
>
> Python 2.5.2 (r252:60911, Jan 20 2010, 21:48:48)
> [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> foo = ['1','2','3']
>>>> bar = ['2','5']
>>>> foo.extend(bar)
>>>> foo = list(set(foo))
>>>> foo
> ['1', '3', '2', '5']
>
> I used to use list comps to do this instead.
>>>> foo = ['1','2','3']
>>>> bar = ['2','5']
>>>> foo.extend([a for a in bar if a not in foo])
>>>> foo
> ['1', '2', '3', '5']
>
> A very long time ago, we all used dictionaries, but I'm not interested
> in that ever again. ;-)
> Is there any performance hit to using one of these methods over the
> other for rather large lists?

Yes.  In the second approach, "if a not in foo" is O(n) if foo is a
list, and since you're doing it m times, that's O(n * m).

The first approach is merely O(n + m).

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is there any advantage or disadvantage to using sets over list comps to ensure a list of unique entries? deathweaselx86 <deathweasel@gmail.com> - 2011-06-20 12:43 -0700
  Re: Is there any advantage or disadvantage to using sets over list comps to ensure a list of unique entries? Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-20 13:59 -0600
  Re: Is there any advantage or disadvantage to using sets over list comps to ensure a list of unique entries? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-21 01:10 +0000
    Re: Is there any advantage or disadvantage to using sets over list comps to ensure a list of unique entries? Ethan Furman <ethan@stoneleaf.us> - 2011-06-20 20:29 -0700
  Re: Is there any advantage or disadvantage to using sets over list comps to ensure a list of unique entries? rusi <rustompmody@gmail.com> - 2011-06-20 20:59 -0700
  Re: Is there any advantage or disadvantage to using sets over list comps to ensure a list of unique entries? Raymond Hettinger <python@rcn.com> - 2011-06-25 01:05 -0700

csiph-web