List comprehension for testing **params

Tim Chase python.list at tim.thechases.com
Sun Nov 11 17:49:50 EST 2012


> assert[key for key in required if key in params.keys()]
...
> Could you explain why it doesn't work and do you have any idea of how it 
> could work ?

Well, here, if any of the items are found, you get a list that is
non-False'ish, so the assert passes.

It sounds like you want all() (available as of 2.5)

  assert all(key in params for key in required)

This is modulo any key normalization for "From" vs. "FROM" vs.
"from", etc; but should be enough to pass your initial requirements.

-tkc





More information about the Python-list mailing list