Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:" ': 0.03; 'subject:test': 0.05; 'dictionary': 0.07; '(unless': 0.09; 'builtin': 0.09; 'dict': 0.09; 'suggestion.': 0.09; 'value:': 0.09; 'solutions,': 0.12; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; '\xc2\xa0if': 0.16; 'possibly': 0.16; 'have:': 0.19; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'cheers,': 0.20; 'maybe': 0.21; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'cc:addr:python- list': 0.22; 'elegant': 0.23; 'unsure': 0.23; 'chris': 0.27; 'message-id:@mail.gmail.com': 0.28; 'testing': 0.28; 'missed': 0.29; 'fri,': 0.29; 'this.': 0.30; 'cc:addr:python.org': 0.31; 'comparison': 0.31; 'key,': 0.31; 'tricks': 0.31; 'second': 0.31; 'it.': 0.31; 'speaking': 0.31; 'all,': 0.31; 'perhaps': 0.32; 'another': 0.32; 'worry': 0.33; 'reference': 0.34; 'occurs': 0.35; 'quite': 0.36; 'none': 0.36; 'some': 0.37; 'apr': 0.38; 'received:google.com': 0.38; 'ways': 0.38; 'but': 0.38; 'comments': 0.39; 'i.e.': 0.40; "it's": 0.40; 'header:Received:5': 0.40; 'simple': 0.60; '2011': 0.62; 'unique': 0.63; 'view': 0.64; 'idiomatic': 0.84; 'inefficient': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=9NrYat98woUVFfA4OXpyPK3Slqv3UkQBQCUib7MQ8Ok=; b=fpw1ld505LTzwM0LOfatEn6sLB3OrDCxDkC0NNbHhI7qDiQCPwDggrZvoe6hfnlZrV CR0iqeJ134ubVtyYJSglE816gNUL1Sl7vdnNsPtoVuwr0a9e+NdeHQXOCW6wOH2tznXM lYDX67kMJHtwmG+fxHxrtm+4o+lpL3LLAbjYE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=cSWx0ellKbus/D4DMu6SPbfxeGFBdS+1aKra1wE7Y1Z8gC+ueXBJ9qk8QRhaLq4R5b JeRN72qfdNeLI5GG8QOfkQRxJwiqUGfslrqxr/LLdwsSU7sUkC9CdjmcP/MdX04g8u0u K7oxwyQS5HeZjidOMlQLUxBV7Z2nnevd/CipU= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: References: Date: Fri, 22 Apr 2011 07:38:38 -0700 X-Google-Sender-Auth: F0_4gDXUmdpodqEn47Z1u5v9FIQ Subject: Re: suggestions, comments on an "is_subdict" test From: Chris Rebert To: Vlastimil Brom Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: 36 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303483121 news.xs4all.nl 41113 [::ffff:82.94.164.166]:35096 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.stben.net!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!feeder.news-service.com!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3874 On Fri, Apr 22, 2011 at 6:55 AM, Vlastimil Brom wrote: > Hi all, > I'd like to ask for comments or advice on a simple code for testing a > "subdict", i.e. check whether all items of a given dictionary are > present in a reference dictionary. > Sofar I have: > > def is_subdict(test_dct, base_dct): > =C2=A0 =C2=A0"""Test whether all the items of test_dct are present in bas= e_dct.""" > =C2=A0 =C2=A0unique_obj =3D object() > =C2=A0 =C2=A0for key, value in test_dct.items(): > =C2=A0 =C2=A0 =C2=A0 =C2=A0if not base_dct.get(key, unique_obj) =3D=3D va= lue: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return False > =C2=A0 =C2=A0return True > > I'd like to ask for possibly more idiomatic solutions, or more obvious > ways to do this. Did I maybe missed some builtin possibility? > I am unsure whether the check =C2=A0against an unique object() or the > negated comparison are usual.? I second MRAB's all() suggestion. The use of object() as a higher-rank None is entirely normal; don't worry about it. Also, the following occurs to me as another idiomatic, perhaps more /conceptually/ elegant possibility, but it's /practically/ speaking quite inefficient (unless perhaps some dict view tricks can be exploited): def is_subdict(sub, larger): return set(sub.items()).issubset(set(larger.items())) Cheers, Chris