Keys in dict and keys not in dict

Chris Angelico rosuav at gmail.com
Mon Mar 19 00:25:48 EDT 2018


On Mon, Mar 19, 2018 at 3:18 PM, Ben Finney <ben+python at benfinney.id.au> wrote:
> Chris Angelico <rosuav at gmail.com> writes:
>
>> Sounds like a set operation to me.
>>
>> expected = {"foo", "bar", "spam"}
>> missing = expected - set(json)
>
> That works (because iterating a dict returns its keys). But it is less
> immediately understandable, IMO, than this::
>
>     expected = {"foo", "bar", "spam"}
>     missing = expected - set(json.keys())
>

Sure, whichever way you want to do it. Either way, it's a set
difference operation that gives the OP's desired information.

ChrisA



More information about the Python-list mailing list