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


Groups > comp.lang.python > #8063

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

Date 2011-06-20 20:29 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject 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> <4dffefa1$0$30002$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.213.1308627006.1164.python-list@python.org> (permalink)

Show all headers | View raw


Steven D'Aprano wrote:
> On Mon, 20 Jun 2011 12:43:52 -0700, deathweaselx86 wrote:
>> I've been converting lists to sets, then back to lists again to get
>> unique lists.
>>
>> 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']
>>
>> Is there any performance hit to using one of these methods over the
>> other for rather large lists?
> 
> Absolutely!
> 
> For small lists, it really doesn't matter what you do. This probably only 
> matters beyond a few tens of thousands of items.

Depends on the complexity of the object.  It only took a couple thousand 
dbf records to notice a *huge* slowdown using 'in' tests on regular lists.

~Ethan~

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