python collections cheat sheet

Solutions on MaxInterview for python collections cheat sheet by the best coders in the world

showing results for - "python collections cheat sheet"
Marc
04 Jun 2017
1sum_of_elements  = sum(<collection>)
2elementwise_sum  = [sum(pair) for pair in zip(list_a, list_b)]
3sorted_by_second = sorted(<collection>, key=lambda el: el[1])
4sorted_by_both   = sorted(<collection>, key=lambda el: (el[1], el[0]))
5flatter_list     = list(itertools.chain.from_iterable(<list>))
6product_of_elems = functools.reduce(lambda out, el: out * el, <collection>)
7list_of_chars    = list(<str>)
8