Iterables: lists, sets, dicts, tuples, strs, iterators these are functions: list, tuple, dict, set, sum, sorted(should be sort), any, all, max, min treat a string as an iterable and you get the chars for x in iterable: filter(boolfunction, iterable) opaque return, note not method functools.reduce(f, iterable) = ((((1, 2), 3), 4), 5) map(f, iterable) returns an opaque iterable yield makes a function behave like an iterable: primes.py, treeen.py Iterators: - like lazy evaluation x = iter(ob) - create an iterator from an iterable for pos, val in enumerate([9,8,7,6]): (it produces tuples) itertools.count() intsfrom default 0, can have ,stepsize itertools.cycle(iterable) infinite repetition of all itertools.repeat(item, times), default times is unending itertools.chain(it1, it2, it3, ...) is append itertools.combinations(it, r) all r-tuples, but after (a,b), (b,a) does not appear, r required itertools.permutations(it, r) same but both (a,b) and (b,a) appear, r not required L = [("a", 1), ("a", 2), ("b", 3), ("b", 4), ("b", 9)] key_func = lambda x: x[0] for key, group in itertools.groupby(L, key_func): # pretty much worthless print(key + " :", group) itertools.accumulate(it {, func=operator.add}) 1 2 3 4 -> 1 3 6 10, optional initial = value zip(iterable, iterable, ...) = iterator next(it) returns next item, exception StopIteration if at end class numbers: / def __iter__(self): / self.val = 1 / return self def __next__(self): x = self.val / self.val += 2 / return x ob = numbers() / i = iter(ob) can raise StopIteration