Re: Keys in dict and keys not in dict
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 efficient. > > -- > INADA Naoki > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Keys in dict and keys not in dict
> 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
Ben Finney wrote: > Chris Angelico <ros...@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()) 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
Re: Keys in dict and keys not in dict
On Mon, Mar 19, 2018 at 3:18 PM, Ben Finney <ben+pyt...@benfinney.id.au> wrote: > Chris Angelico <ros...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Keys in dict and keys not in dict
Chris Angelico <ros...@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()) -- \ “The greater the artist, the greater the doubt; perfect | `\ confidence is granted to the less talented as a consolation | _o__) prize.” —Robert Hughes | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: Keys in dict and keys not in dict
On Mon, Mar 19, 2018 at 2:32 PM, Andrew Zwrote: > 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 did it again ...") Sounds like a set operation to me. expected = {"foo", "bar", "spam"} missing = expected - set(json) if missing: print("Missing keys:", missing) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Keys in dict and keys not in dict
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 did it again ...") thank you AZ -- https://mail.python.org/mailman/listinfo/python-list