Skip to content
academia.sh

Course Intermediate

Data Structures and Functional Tools

By the end of this course

Start course

01

Collections

The costs of a mutable sequential collection, the guarantee immutability brings, key-value mapping and ordering behavior, set operations, specialized containers, and what slicing copies versus shares.

  1. 01 Lists A list holds not the items but the links to them; putting a thousand items in creates 1000 objects but binding the list to a second name creates 0, appending the same item five times creates 1, and only three of eight growth forms build a new container.
  2. 02 Tuples Three of six operations fail on a tuple with an exception, but the operation that mutates an inner item still passes on a tuple; immutability is one layer deep, and in exchange the container gains the capability to be a key, as seen in three of five candidates.
  3. 03 Dictionaries Searching for the same item among two hundred does 200 comparisons in a list but 1 in a set and a dict; asking for keys returns not a copy but a view, and it sees a key added afterward — going from 2 to 3, while a list snapshot stays at 2.
  4. 04 Sets Deduplicating three hundred items does 15050 comparisons scanned by hand, 200 handed to a set, and all three give the same hundred items; all six set operations create 0 new items, and four build a new container while two mutate the existing one.
  5. 05 Specialized Collections Writing the same grouping with setdefault creates 320 containers, with a defaultdict 100; on a thousand-item stream, all three spellings produce 1000 objects but held is 1000 against 5, and rotating builds three containers with a list against none with a deque.
  6. 06 Slicing All eight slice forms create 0 new items and return a new container; a nested list produces 4 items and both inner lists are shared in the slice copy — the outer container is new, the contents are old.

02

Iteration and Functional Tools

The iterator protocol, lazy evaluation, memory-friendly transformation chains, three comprehension forms, passing a function as an argument, and preserving metadata while wrapping behavior.

  1. 01 Iterator Protocol You do not need to rebuild the container to see its items: a slice copy writes three references into a new list while an iterator writes zero, shares all three, and materializes no item.
  2. 02 Generators Calling a generator function does not run a single line of its body: on a thousand-item stream, setup materializes 1000 objects for a comprehension, 0 for a generator, and stays at 3 once three items are requested.
  3. 03 Generator Expressions Summing a thousand items, a generator and a comprehension both produce 1000 objects and both give 499500; the only place they split is what stays held in a list — 0 against 1000. Laziness defers production, it does not avoid it.
  4. 04 Comprehension Syntax From the same thousand items, a list comprehension retains 1000, a set and a dict comprehension retain 100; whether a filter condition is written before or after production changes the materialized count from 5 to 10.
  5. 05 Lambda and Higher-Order Functions A function is an object and can be passed as an argument: on two hundred items, the key function for sorted, min, and max is called exactly once per item — a hand-written sort doing the same job calls it 39800 times.
  6. 06 Decorators Wrapping does not delete the original function, it adds a layer on top: one decorator builds two layers, two decorators build three, and when metadata is not preserved, the function's name, doc, and signature change to the wrapper's.

03

Modules and Packages

Import mechanics and the search path, subpackages and relative imports, the standard library's scope, and when the script/module duality runs the body.

  1. 01 The Module System Import is an act of creation: when the same module is requested three times, two objects get created, and the body runs once while the cache is full; search path order only decides while the cache is empty.
  2. 02 Package Structure The same short name resolves to the neighbor in relative notation and to the root in absolute notation, giving two separate objects; a single submodule request runs five bodies and writes five module objects to the cache.
  3. 03 A Tour of the Standard Library Writing the same job with the standard library versus by hand is measured on three axes: in the stream job the line count stays equal but objects created are 800 against 3, in name splitting 2 of six names diverge, in adding days 1 of six dates silently diverges.
  4. 04 Reloadability and the Main Block The same file creates one module object when imported and a second when run as a script, and the main block runs only in the second; reload keeps the module object but renews every function inside it.

Start typing to search.

↑↓ Esc navigate · open · close