Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.102 X-Spam-Level: * X-Spam-Evidence: '*H*': 0.80; '*S*': 0.00; 'python': 0.07; 'solution,': 0.09; 'contrary,': 0.16; 'runs,': 0.16; 'subject:Converting': 0.16; 'subject:set': 0.16; 'transforming': 0.16; 'subject:list': 0.22; 'header:In-Reply-To:1': 0.22; 'loop': 0.22; 'equivalent': 0.26; 'message-id:@mail.gmail.com': 0.28; 'daniel': 0.29; 'list': 0.30; 'seem': 0.30; 'operation.': 0.31; 'to:addr:python-list': 0.32; 'another': 0.32; 'received:209.85': 0.37; 'received:google.com': 0.38; 'less': 0.38; 'but': 0.38; 'set': 0.39; 'to:addr:python.org': 0.39; 'received:209': 0.39; 'header:Received:5': 0.40; 'might': 0.40; 'best': 0.60; 'back': 0.61; 'conversion': 0.64; '100': 0.70; 'concerns': 0.80; 'marginally': 0.84; 'received:209.85.210.174': 0.84; 'received :mail-iy0-f174.google.com': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=++GXZT3YhKVf5ASdZLLXfdW8IK9puBoQobH6iauZ/JA=; b=Ondw5W25xrUpEwY7ue4/vCf/ttj5LJKyqF6Dr8bZ4I4FMOWeKd4aVvjqFnRiik5YNY tkpbCpHsZPgx8kmtqsRBIRVNyyLFio5h5A63zBg+dNTZTXKJpHukVoUxw3g5gawxwgdb Dw8E51PwsfXs85mks7NDDdxbbeyWtuZrsSCYk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=bfpkydOkEuFK2vknmZPOjoNW5TzpYskgyTKXVVk3xaEqWhyn+CnzllYgzG0uVkIGci lbimFpD/DeeW+aRLVSd/NsReGXzDjDYXYFmEqHfSv5Wq0RiZZv3zi1gD8hj6KGA1bY6+ B4hf79R1xmgDtQPtpYvHXqxLGUoKAuYbXZqls= MIME-Version: 1.0 In-Reply-To: References: <87iptdid68.fsf@benfinney.id.au> <871v00j2bh.fsf@benfinney.id.au> <34fc571c-f382-405d-94b1-0a673da5f46b@t16g2000vbi.googlegroups.com> Date: Mon, 16 May 2011 13:37:17 +1100 Subject: Re: Converting a set into list From: Daniel Kluev To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 24 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305513441 news.xs4all.nl 81481 [::ffff:82.94.164.166]:58167 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5465 > Both solutions seem to be equivalent in that concerns the number of needed loop runs, but this two-step operation might require one less loop over list1. > The set&set solution, in contrary, might require one loop while transforming to a set and another one for the & operation. python -m timeit -s "l1 = range(1, 10000); l2 = range(5000, 15000)" "l3 = list(set(l1) & set(l2))" 100 loops, best of 3: 2.19 msec per loop python -m timeit -s "l1 = range(1, 10000); l2 = range(5000, 15000)" "s=set(l2); l3 = [i for i in l1 if i in s]" 100 loops, best of 3: 2.45 msec per loop python -m timeit -s "l1 = range(1, 100000); l2 = range(50000, 150000)" "l3 = list(set(l1) & set(l2))" 10 loops, best of 3: 28 msec per loop python -m timeit -s "l1 = range(1, 100000); l2 = range(50000, 150000)" "s=set(l2); l3 = [i for i in l1 if i in s]" 10 loops, best of 3: 28.1 msec per loop So even with conversion back into list set&set is still marginally faster. -- With best regards, Daniel Kluev