Data structures and functions
Lists, dictionaries, sets and functions.
Level 101 · 17 lessons · 15 practice labs
Lessons
Ordered data
- Lists: You will learn to create lists, ordered collections of data, and to add, remove and walk through their items.
- List methods and copying pitfalls
- Tuples: You will understand tuples, which cannot be changed afterwards, and when they suit better than lists.
- Unpacking and starred assignment
- Short task: a command-line to-do list
Sets and dictionaries
- Sets: You will learn sets, which drop repeated items on their own, and how to find what two sets share or do not share.
- Dictionaries: You will learn dictionaries, which store information as key and value pairs, and how to read and update them.
- Dictionary views, merging and safe access with get
- collections: Counter, defaultdict, deque and namedtuple
- List and dictionary comprehensions: You will learn to build lists and dictionaries in a single short, readable line.
- Short task: the most frequent words in a text
Nested data and choosing well
- Nested data structures: You will nest lists and dictionaries to model real-world data and learn to move through the layers.
- JSON and YAML: You will learn to read and write configuration data in JSON and YAML formats with Python.
- Sorting and searching: You will learn to sort data and to find a particular item in it quickly.
- Choosing the right structure: speed, memory and readability
- Short task: merge a list of users and groups
Capstone
- Capstone: an inventory manager: You write a small program that keeps a fictional company's device inventory in lists and dictionaries, with add and search features.
Practice labs
- Building record dictionaries from headers and rows: Turn a header list and row lists into dictionaries with zip, filling missing fields of short rows with None and dropping extras.
- Picking the top five scores: Write a function that sorts and slices a score list to return the five highest and five lowest scores, even when the list is shorter than five.
- Finding the shallow copy bug: Find the bug where nested lists copied from a template table change together and fix it with a deep copy; the tests check the template stays intact.
- Parsing records with starred assignment: Write a function that splits shipment rows with a variable number of fields into first, last and middle parts using unpacking and starred assignment, rejecting short rows.
- Finding students in both classes: Write a function that uses set operations to return the students in both lists, those in only one, and all unique students.
- Merging default and user settings: Write a function that merges default settings with user settings using the | operator and get, and reports unknown keys.
- Inverting a dictionary with a comprehension: Write a function with dictionary and list comprehensions that turns a staff-to-department mapping into a sorted staff list per department.
- Finding permission changes with sets: Write a function that derives added, removed and changed permissions from Çınaraltı Library's user-permission maps for yesterday and today, passing randomised tests.
- Counting votes with Counter: Write a function that counts survey answers with Counter and returns the top three options, breaking ties alphabetically.
- Grouping orders by customer: Write a function that uses defaultdict to turn Kestane Market's order list into a total amount and order count per customer.
- Computing a moving average with deque: Write a class that updates the average of the last N sensor readings in constant time per step using deque(maxlen) and pass tests with large random streams.
- Turning rows into namedtuple objects: Write a function that turns flight table rows into namedtuples, sorts delayed flights by delay and returns missing or malformed rows in a separate list.
- Safely reading a value from nested config: Write a function that follows a dotted path like 'db.primary.port' through nested dictionaries and returns a default when a key is missing.
- Sorting JSON records by several keys: Write a function that sorts staff records read from JSON by department ascending and seniority descending; records missing the seniority field count as zero and ties are broken by name.
- Speeding up a list lookup with a set: Speed up a slow loop that searches a blocked-IP list across tens of thousands of records by choosing the right structure; tests check both correctness and a time limit.
About this course
Lists, dictionaries, sets and functions.
What you will learn
- Ordered data
- Sets and dictionaries
- Nested data and choosing well
- Capstone
Prerequisites
No prior knowledge is needed; the course starts from zero.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at beginner level; every term is explained where it first appears.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.