Python list exercises
Lists look simple until a loop runs one step too far or two variables turn out to be the same list. These exercises are short functions that work with lists, each with one real bug and tests that prove your fix.
They cover what trips beginners most: indexes and slices, methods that change the list and return None, and copies that are not copies.
The lists bugs you will meet
- The last index is len(items) - 1, and a slice's end is never included.
- append() and sort() change the list and return None, so items = items.sort() loses everything.
- b = a does not copy a list; both names point at the same one.
- Removing items while looping over the same list skips elements.
- [[0] * 3] * 3 makes three references to one row.
The exercises
Easy
- Middle item lookup crashes with TypeErrorType
- Sorted name list comes back as NoneMutation and copying
- Adding a tag makes the whole list disappearMutation and copying
- AI Attempt: The palindrome checkerType
- Countdown never reaches zeroOff-by-one
- Cart total adds up positions, not pricesLogic
- Leaderboard shows the three worst playersLogic
- Search gives up after checking one itemLogic
- Comparing each day with the next crashes at the endOff-by-one
- Contact list puts capital names firstLogic
- Adding two items puts a list inside the listMutation and copying
- Shopping list numbering starts at 0Off-by-one
- Queue serves the last person firstLogic
- PIN builder crashes on a list of digitsType
- Ranking sorts students by name, not scoreLogic
- Removing even numbers while looping skips adjacent itemsLogic
- Sorting list with sort method returns None instead of listType
- Appending sublist of roles nests array instead of extendingType
- Popping from empty stack raises IndexError without checkLogic
- Sum function with string elements raises TypeErrorType
- Finding minimum on empty transaction list raises ValueErrorLogic
- Dictionary fromkeys shares default mutable list across all keysLogic
- Reversing list in place with reverse method returns NoneType
- Longest-word finder returns the shortestLogic
- Ingredient list ends with a stray commaOff-by-one
- Playlist reverser crashes with IndexErrorOff-by-one
- Removing the first song crashesType
- Sorted score list returns NoneNull and undefined
- List index lookup crashes on odd lengthType
- First three characters slice drops the third letterOff-by-one
Medium
- Countdown never stopsInfinite loops
- Highest score picks "9" over "10"Type
- Biggest gap between numbers comes out as -1Logic
- Second list still has the first call's itemScope and closure
- AI Attempt: The Two Sum TrapLogic
- Spam filter removes only the first "spam"Logic
- AI Attempt: The runner-up finderLogic
- Booking one seat books the whole columnMutation and copying
- Role check lets "adm" throughType
- Duplicate finder reports each duplicate twiceLogic
- Swapping rows and columns drops a columnOff-by-one
- Pair finder returns the last pair, not the firstLogic
- Class variable list leaks items across multiple user cartsScope and closure
- Multiplying row list in matrix creates identical row referencesScope and closure
- New team member vanishes from a new teamMutation and copying
- Empty test run reported as all passingLogic
- AI Attempt: Merging two sorted listsLogic
- Express-only shipping comes out as single lettersType
- AI Attempt: Longest common prefixOff-by-one
- Times table prints the same row every timeScope and closure
- Giving one student a grade gives everyone that gradeMutation and copying
- Recursive total crashes on the way backNull and undefined
- AI Attempt: Flattening stops one level deepLogic
Hard
- Binary search can't find the last itemOff-by-one
- Two shoppers end up with the same basketScope and closure
- Removing even numbers leaves one behindMutation and copying
- New basket already has the first basket's itemScope and closure
- Splitting into groups loses the last groupOff-by-one
- Best window of 2 misses the last pairOff-by-one
- Editing a copied grid changes the originalMutation and copying
Errors you might be searching for
More Python practice by topic
Questions
Do I need to know loops first?
Basic for loops help. If you are newer than that, start with the easy ones at the top: several need no loop at all.
Is there a certificate?
Yes, free. Fixing 25 easy Python bugs while signed in earns Python Debugging · Level 1, and the 30-day Python path has its own certificate.