re-creating underscore.js functions in python