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!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:two': 0.07; 'subject:compare': 0.09; 'am,': 0.14; 'received:209.85.214.174': 0.14; 'received:mail-iw0-f174.google.com': 0.14; 'wrote:': 0.14; 'angelico': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'intersection': 0.16; 'subject:similar': 0.16; 'tue,': 0.17; 'header:In-Reply-To:1': 0.21; 'variable': 0.21; 'message-id:@mail.gmail.com': 0.28; 'received:209.85.214': 0.28; 'subject:?': 0.29; 'lists': 0.29; 'to:addr:python-list': 0.33; 'operations': 0.33; 'chris': 0.34; 'subject:What': 0.35; 'using': 0.35; 'subject:lists': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.37; 'two': 0.37; 'subject:: ': 0.38; '8bit%:6': 0.39; 'skip:s 20': 0.39; "i'd": 0.39; 'received:209': 0.39; 'sets': 0.39; 'to:addr:python.org': 0.39; 'making': 0.67; 'sets,': 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:content-transfer-encoding; bh=IxXI1kq00G80sMfQOWKj8cJKGNR13L1XyM+Zm+XNPW4=; b=FZBmv/9SExAXyO4KcCmJCrh5Frsku2/7pWZyl+AbCfUQ2H+HRjnhGCQZcZ7k4hxPeN pdbc8Na/R2RSHrlHOw09mS1rSH7EBVAWZPHiOXIaVR9OG9vPbSBfPil4FRYkgZ3+GmVL mjZ5vdQHOmiX06d38N87AoCiUptf6rYZXJw6o= 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:content-transfer-encoding; b=OrQeD3QsaUYblcZg+bbVnR9WRkGYyGF9g04xQpuL1dO6mPR9n9+HtzlgOWjRvRXoZ9 ktj4SxE1QiL1TdsX2O9fEzsNWxEvVgZZCthzOhB1L/l+c0Eta8GEPLkuCTusUP4g77c0 tbSu3B2w28F36dAQVJ8+tdDkePZuMcqOzzCbw= MIME-Version: 1.0 In-Reply-To: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> References: <3ff2f4cc-7c26-4f59-9463-d6f0874174d8@dn9g2000vbb.googlegroups.com> Date: Tue, 14 Jun 2011 01:09:07 +1000 Subject: Re: What is the most efficient way to compare similar contents in two lists? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 16 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307977750 news.xs4all.nl 49048 [::ffff:82.94.164.166]:59731 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7519 On Tue, Jun 14, 2011 at 12:58 AM, Zachary Dziura wrote= : > if set(source_headers) =3D=3D set(target_headers): > =A0 =A0similar_headers =3D len(source_headers) Since you're making sets already, I'd recommend using set operations - same_headers is the (length of the) intersection of those two sets, and different_headers is the XOR. # If you need the lists afterwards, use different variable names source_headers =3D set(source_headers) target_headers =3D set(target_headers) similar_headers =3D len(source_headers & target_headers) different_headers =3D len(source_headers ^ target_headers) Chris Angelico