def find_one(func, array): ''' Utility function Return the first element in array for which func returns True. ''' for e in array: if func(e): return e return None def find_all(func, array): ''' Utility function Return all elements in array for which func returns True. ''' return [e for e in array if func(e)] def dict_get_fallback(d, key, fallback): ''' Utility function Returns d[key] if key exists. Else, return fallback ''' return d[key] if key in d.keys() else fallback