def trimmed_sum(x, limit): """prec: x is a numerical list postc: finds the sum of all elements of x <= limit""" out = 0 for k in x: if k <= limit: out += k def trimmed_sum_better(x, limit): return sum([ k for k in x if k <= limit]) def foo(x): """prec: x is a numerical list. postc: returns the sum of the cubes of the nonnegative elements o x. """ return sum([i**3 for i in x if i >= 0]) x = [-3, 4, -2, 8] print(foo(x) == 576) zoo = {"cat": 5, "dog":3, "tapir":9, "okapi":2, "hyena":1} rare = {k:v for k,v in zoo.items() if v <= 2} print(rare)