Ben Finney wrote:
> Chris Angelico <[email protected]> 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())
There's no need to materialize the set of keys:
>>> expected = {"foo", "bar", "ham"}
>>> json = dict(foo=1, bar=2, spam=3)
>>> expected - json.keys()
{'ham'}
In Python 2 use json.viewkeys() instead of keys().
--
https://mail.python.org/mailman/listinfo/python-list