Python · Module 3
Data Structures
Take each lesson, answer its quiz, then do its practice. After the last lesson come the module quiz, the project and the interview questions.
Lessons
- 3.1ListsStore ordered, changeable collections and slice them.
- 3.2TuplesUse fixed-size immutable records, and unpack them.
- 3.3SetsKeep unique items and ask membership questions fast.
- 3.4DictionariesMap keys to values: counting, grouping, lookup.
- 3.5StringsClean and split text, the first step of every text model.
- 3.6ComprehensionsBuild lists, sets and dicts in one readable line.
Module material
Take these after the last lesson, in this order. Next on each page leads through them.
- Module quiz15 questions across the whole module. Take it after the last lesson; your first pick on each question is the one that counts.
- Project: The Most Common Words in Product ReviewsStated as a problem, with no template and no step-by-step instructions. Working out the steps is the point.
- NotebooksOpen them in Google Colab. Each answer is checked as you go: correct, wrong with the expected value, or not answered yet.
- Cheat sheetLists, tuples, dictionaries and sets on one page: the terms, which structure to use, the syntax at a glance, the rules and the common mistakes. Free to read and share, no account needed.
- Interview questions25 questions interviewers ask on data structures, from first principles to the follow-ups. The first 3 model answers are free to read; Pro opens the rest.
Glossary
Every term the module introduces, with the lesson that introduces it.
- alias
- A second name bound to the same object: after
backup = cart,backupis an alias ofcart. 3.1 - element
- One value stored in a list. 3.1
- index
- An element's position, counting from zero. A negative index counts from the end, from
-1. 3.1 - method
- A function that belongs to an object, called with a dot:
cart.append(...),review.lower(). 3.1 - mutable
- A type whose objects can be changed after they are created; a list is mutable, and so are sets and dictionaries. 3.1
- mutation
- Changing an object in place, which every alias sees:
append,remove,sort, assigning to an index. 3.1 - rebinding
- Making an existing name refer to an object by assigning to that name. 3.1
- shallow copy
- A new outer collection that still refers to the original elements; nested mutable elements remain shared. 3.1
- slice
- A range of elements, from a start up to but not including a stop, with an optional step:
temps[2:5],temps[::-1]. Slicing a list creates a new outer list; slicing a tuple or string returns the same type, possibly reusing an unchanged immutable object. 3.1 - slice step
- The spacing and direction used to select positions in a slice. 3.1
- immutable
- An object that can not be changed after it is created: a tuple, a string, a number. 3.2
- packing
- Collecting several values, separated by commas, into one tuple:
point = 12.9716, 77.5946. 3.2 - tuple
- An ordered sequence of values, written with commas, that can not be changed:
(12.9716, 77.5946). 3.2 - unpacking
- Binding each item of a sequence to its own name, in order:
lat, lon = point. 3.2 - difference
- The values in the first set that are not in the second,
a - b. 3.3 - frozenset
- An immutable, hashable set:
frozenset({1, 2}). 3.3 - hash
- A number Python computes from a value, which a set uses to decide where the value is stored. A dictionary uses its keys' hashes in the same way. 3.3
- hashable
- A value whose hash stays stable during its lifetime and whose equal values have equal hashes, so it can be a set member or dictionary key: numbers, strings, and tuples of hashable values. Lists, sets and dictionaries are not. 3.3
- intersection
- Every value in both sets,
a & b. 3.3 - KeyError
- The exception for a missing dictionary key or an absent member passed to
set.remove. 3.3 - set
- An unordered collection in which each value appears at most once. An empty set is
set(). 3.3 - Counter
- A dictionary that counts; a missing key counts zero.
Counter(votes).most_common(1)gives[('tea', 3)]. Fromcollections. 3.4 - defaultdict
- A dictionary that creates a missing key's value when the key is read:
defaultdict(list)creates[]. Fromcollections. 3.4 - dictionary
- A collection of key value pairs, looked up by key:
{"Pasta": 12.50, "Salad": 8.25}. Its type isdict. 3.4 - insertion order
- The order keys were first added to a dictionary; updating a value preserves its position. 3.4
- key
- What a dictionary looks a value up by, such as
"Pasta"in the menu. Each key appears once, and must be hashable. 3.4 - key value pair
- One entry of a dictionary: a key and its value, written
"Pasta": 12.50. 3.4 - nested dictionary
- A dictionary whose values are dictionaries:
cafe["drinks"]["Juice"]. 3.4 - value
- What a dictionary stores for each key, such as
12.50for"Pasta". Values can be anything, including lists and dictionaries. 3.4 - chaining
- Calling a method on the value the previous method returned:
review.strip().lower(). 3.5 - iterator
- An object that yields its items one at a time when asked for the next item. 3.5
- palindrome
- Text that reads the same forwards and backwards. 3.5
- separator
- Text used between fields when splitting or joining strings. 3.5
- string
- An immutable sequence of characters, indexed and sliced like a list. 3.5
- substring
- A contiguous sequence of characters within a string. 3.5
- token
- One of the pieces tokenisation produces. Our tokeniser lowercases text, removes only
.,,,!and?, then splits on whitespace; other punctuation may remain. 3.5 - tokenisation
- Splitting text into pieces. Language models split into subword pieces; the video's tokeniser splits on whitespace. 3.5
- tokeniser
- A procedure that splits text into tokens. 3.5
- Unicode
- The standard for representing characters in Python strings; one indexed item is one code point and may be only part of a displayed character. 3.5
- whitespace
- Spacing characters such as spaces, tabs and newlines. 3.5
- dictionary comprehension
- A dictionary built from a key and a value evaluated for each item:
{item: price for item, price in zip(items, prices)}. 3.6 - filter
- A condition that determines which items are included. 3.6
- generator expression
- An expression that yields values on demand, such as
sum(s for s in week if s >= 10000), without building a list first. 3.6 - list comprehension
- A list built from an expression evaluated for each item of an iterable:
[round(p * 1.08, 2) for p in prices], with an optionalifat the end to filter. 3.6 - scope
- The region of a program in which a name is available; a comprehension's loop variable has its own scope. 3.6
- set comprehension
- A set built from an expression evaluated for each item:
{len(item) for item in items}. 3.6 - side effect
- An action such as printing or mutating an object, beyond producing a return value. 3.6