</>
Vizly

Python Data Structures: List, Tuple, Dict, Set

July 3, 20263 min
PythonFundamentalsPracticeBeginner

Chapter 4 of Python Fundamentals. 14 practice drills on lists, tuples, dicts, and sets: slicing, copying, hashability, and the aliasing traps. Predict the output before you run anything.

The rules, again

Same game as chapter 1: read the question, commit to a prediction, run it, and only then open the answer. Wrong prediction means the drill goes on your redo list for three days from now.

This is the highest-stakes chapter in the core tier. Lists, tuples, dicts, and sets are what you touch in every interview problem, and their traps are the ones that fail submissions: .sort() returning None, [[0]*3]*3 making one row wear three hats, shallow copies that are not copies where it counts.

Learn the concepts first

Rusty on the basics? Work through Lists and Dictionaries on learnpython.org first, then come back and drill.


One idea before you start

Half of this chapter turns on one word from the world of dicts and sets.

Definition

An object is hashable when it has a hash value that never changes during its lifetime. Dicts and sets use that hash to find things instantly. Immutable objects like str, int, and tuple are hashable; mutable ones like list, dict, and set are not, because a changed object would be lost inside its own table.


The drills

Click a question to open it. The answer stays hidden until you ask for it. Predict first.

14 drills. Predict first, then open the answer

Want more drills?

When these stop surprising you, let an AI generate fresh ones. Copy this prompt into ChatGPT, Claude, or any assistant you like:

You are my Python drill coach. Quiz me on: lists, tuples, dicts, and
sets. Slicing, copying vs aliasing, shallow vs deep copy, hashability,
set operations, unpacking, and dict merging.
 
Rules:
- Ask ONE question at a time. Show a short code snippet and ask me to
  predict the output (or the error) before running it.
- Wait for my answer. Do not reveal anything until I reply.
- If I am right, say so in one line and raise the difficulty slightly.
- If I am wrong, explain the why in 2-3 sentences, then give me a
  similar question later to check I really got it.
- Prefer tricky edge cases over textbook questions: nums = nums.sort(),
  [[0]*3]*3, t[0] += [4] on a tuple, .copy() with nested lists,
  (1) vs (1,), lst += "ab", dict merge order, slicing past the end.
- Keep score. After 10 questions, list the concepts I missed so I can
  add them to my redo list.
 
Start with a medium question.

The predict-first rule matters more than the tool. Whatever generates the questions, always commit to an answer before you look.


Done?

Score yourself honestly and queue up your misses. The tuple += drill (number 7) is the most counterintuitive thing in core Python, an error that mutates anyway. If you predicted that one cold, you understand Python's object model better than the person interviewing you.

Next chapter: functions, where default arguments remember things they should not and *args finally clicks.

Edit this page on GitHubโ†—