0

I have the following python script

def main():
    json_obj = {
        "attributes": [
            {
                "key": "some key",
                "value": "hello"
            }
        ]
    }

    needle = "hello"
    if needle in json_obj["attributes"]:
        print("yay")


if __name__ == "__main__":
    main()

Now i don't understand why the print statement returns false. surely needle is in the list.

santino98
  • 95
  • 1
  • 6

1 Answers1

0

This is probably a duplicate but you are doing an in check in a dictionary which on default only checks the keys:

"key" in  {"key": "some key", "value": "hello"}

will yield false since it is similar to:

"key" in  {"key": "some key", "value": "hello"}.keys()

whereas

"hello" in  {"key": "some key", "value": "hello"}.values()

will yield True

PhilippSelenium
  • 302
  • 2
  • 10