Loading topic
dict and list handle most jobs. Some jobs come up so often that writing them by hand with those two types gets repetitive or slow. The collections module is a standard-library module that packages those jobs as ready-made container types. This chapter shows the problems they 4 min readdict and list, and why dictionary keys must be hashable. You will learn how to create a Counter, how to read counts from it, and which mistakes to avoid.3 min readCounter and read counts from it, as covered in the previous chapter. It also assumes you know how dict.update() behaves. Here you will rank counts, change them in place, and combine whole counters.3 min readdict and list (key lookup, append), and know why only hashable objects can be dict keys. It also assumes you can import names from the standard library.3 min readdefaultdict(list) and defaultdict(int) for grouping and counting, and that you know dict lookup with [] and get(). Here you will learn what happens inside a defaultdict when a key is missing, and which mistakes follow from it.3 min readlist and dict work, and how to import names from the standard library. Here you learn the deque operations that let one container act as a queue or a stack.3 min readdeque operations (append, appendleft, pop, popleft) and the difference between a list and a queue. It covers two features: the maxlen argument, which bounds a deque, and rotate, which shifts its items in place.3 min readlist operations (append, pop, insert, indexing, slicing) and the deque methods from the earlier chapters (append, appendleft, pop, popleft). It also assumes you have read the idea of Big-O notation, which describes how the cost of an operatio4 min readCounter, defaultdict, and deque. It also assumes you are comfortable with dict and list. Here you will turn that knowledge into a decision: given a task, which container should you reach for?3 min read