my_dict = {1: "one", 2: "two", 3: "three"} # PHP array_key_exists() functionlity if 2 in my_dict: print "Found It!" else: print "Whoops, not there!" # PHP in_array() functionality for dictionaries if "two" in my_dict.itervalues(): print "Found It!" else: print "Whoops, not there!" my_list = ["foo", "bar", "baz"] # PHP in_array() functionality for lists if "foo" in my_list: print "Found It!" else: print "Whoops, not there!" # find in objects class MyObj: def __init__(self, name, num): self.name = name self.num = num my_objects = [MyObj('foo', 1), MyObj('bar', 2), MyObj('baz', 3)] if any(obj.name == "foo" for obj in my_objects): print "Found It!" else: print "Whoops, not there!"
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question