Python Data Structures for Data Work
Choose between lists, dicts, sets and tuples with confidence and shape records the way data work needs.
A taste of a lesson
I have a list of orders as dicts. How do I get total spend per customer?
Group and add as you go. Use a dict where the key is the customer and the value is a running total. With defaultdict you can skip the is this key new check: totals = defaultdict(float), then for o in orders: totals[o['customer']] += o['amount']. Each order adds its amount to that customer's total. If some orders might lack an amount, use o.get('amount', 0) instead. Quick check: what would totals contain after two orders from the same customer worth 10 and 15?
Written with AI under Skill Lab's direction as an example. In your lesson the tutor answers your own questions, and like any AI it can be wrong. Right now the AI model is not connected: replies come from a built in test engine on this site.
What you will be able to do
- Pick the right container for a data task and explain why
- Count, group and deduplicate records with dicts, sets and collections helpers
- Reshape data between rows (list of dicts) and columns (dict of lists)
- Navigate nested JSON safely with defaults and type checks
- Avoid aliasing, mutable default and change while looping bugs
Lesson plan
- 1 Lists and tuples for ordered data Use lists for changing sequences and tuples for fixed records. Start
- 2 Dicts for lookup and records Store and retrieve data by key, and represent one record as a dict. Start
- 3 Sets for uniqueness and comparison Deduplicate values and compare groups with set operations. Start
- 4 Counting and grouping Summarise records by category with Counter and defaultdict. Start
- 5 Rows, columns and nested JSON Reshape records and read nested structures without crashing on missing fields. Start
- 6 Bugs that come from references Recognise and fix aliasing, copying and mutable default argument bugs. Start
Try asking
About this tutor
For beginners who know basic Python syntax but freeze when real data arrives as a list of records, nested JSON or a column of values to deduplicate. You learn the four core containers through data tasks: grouping, counting, looking things up, removing duplicates and reshaping between a list of dicts and a dict of lists. The tutor shows comprehensions, the collections helpers for counting and grouping, and the classic traps such as mutable default arguments and changing a list while looping over it. You finish ready to move on to pandas, knowing what it does for you underneath.
Reviews
Students can review a tutor after a paid lesson. Nobody has yet.
More like this
Other tutors on the same or nearby topics.