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

Ethan Furman ethan at stoneleaf.us
Mon Jun 20 23:29:02 EDT 2011


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~



More information about the Python-list mailing list