Re: Keys in dict and keys not in dict

2018-03-19 Thread Andrew Z
Beautiful. Thank you Chris, Ben, Peter and Inada. On Mar 19, 2018 3:14 AM, "INADA Naoki" wrote: > > expected = {"foo", "bar", "spam"} > > missing = expected - set(json.keys()) > > > > dict.keys() returns set-like object. > So `missing = expected - json.keys()` works fine, and it's more e

Re: Keys in dict and keys not in dict

2018-03-19 Thread INADA Naoki
> expected = {"foo", "bar", "spam"} > missing = expected - set(json.keys()) > dict.keys() returns set-like object. So `missing = expected - json.keys()` works fine, and it's more efficient. -- INADA Naoki -- https://mail.python.org/mailman/listinfo/python-list

Re: Keys in dict and keys not in dict

2018-03-19 Thread Peter Otten
Ben Finney wrote: > Chris Angelico 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:: > > expecte

Re: Keys in dict and keys not in dict

2018-03-18 Thread Chris Angelico
On Mon, Mar 19, 2018 at 3:18 PM, Ben Finney wrote: > Chris Angelico 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, IM

Re: Keys in dict and keys not in dict

2018-03-18 Thread Ben Finney
Chris Angelico 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"} missin

Re: Keys in dict and keys not in dict

2018-03-18 Thread Chris Angelico
On Mon, Mar 19, 2018 at 2:32 PM, Andrew Z wrote: > hello, > > i'd like to check if a function parameter (json) has all the keys I expect > it to have and if it doesn't - point out the one that is missing. > > What's the good way of doing that? > > "good way" - something concise... i'd like to av

Keys in dict and keys not in dict

2018-03-18 Thread Andrew Z
hello, i'd like to check if a function parameter (json) has all the keys I expect it to have and if it doesn't - point out the one that is missing. What's the good way of doing that? "good way" - something concise... i'd like to avoid using : if key in json: #pass else print(" Oops, i