Skip to content

Instantly share code, notes, and snippets.

@sneeu
Created February 17, 2010 12:23
Show Gist options
  • Save sneeu/306550 to your computer and use it in GitHub Desktop.
Save sneeu/306550 to your computer and use it in GitHub Desktop.
What I thought was the first non-trivial example I'd found of PHP being shorter than Python. Saved by Twitter.
<?
/**
* getValues(array('a' => 1, 'b' => 2), array('b', 'c')); # => array('b' => 2)
*/
function getValues($data, $fields)
{
return array_intersect_key($data, array_flip($fields));
}
def values(data, fields):
"""
>>> values({'a': 1, 'b': 2}, ['b', 'c'])
{'b': 2}
"""
return dict((k, data[k]) for k in fields if k in data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment