I have a dictionary which may or may not have one or both keys 'foo' and 'bar'. Depending on whether both or either are available, I need to do different things. Here is what I am doing (and it works):
foo = None bar = None if 'foo' in data: if data['foo']: foo = data['foo'] if 'bar' in data: if data['bar']: bar = data['bar'] if foo is not None and bar is not None: dofoobar() elif foo is not None: dofoo() elif bar is not None: dobar() This seems too verbose - what is the idiomatic way to do this in Python (2.7.10)?
foo = data.get('foo')dofoobar()is different fromdofoo()followed bydobar()barandemailor do you really check two different keys?