Python List Practice Problems
The list is the data structure you use most in Python. Learn it in 58 problems, from adding and removing items to sorting, the traps of copying, and 2D lists.
1. Creating and reading #1–17
Indexes start at 0, and a[-1] is the last element.
Reading an element that does not exist raises IndexError.
Ask whether a value is in the list with x in a, and use enumerate(a) when you want the index and the value together.
Syntax refresher: Python for Loop · Python enumerate(): Loop with Index and Value
- Largest number and its position8P0005
- Is it on the list?8P0059
- Score statistics8P0003
- Travel time on a one-way bus route8P0224
- Masking banned words in chat8P0194
- Stamping a grid8P0061
- A tournament where winning tires you8P0180
- A frog on stepping stones8P0193
- What the recipes are missing8P0237
- Finding a melody fragment in a song8P0437
- Spiral fill8P0128
- Vertical survey histogram8P0150
- Shaping a list by passing rule functions8P0239
- Display board cells counted from both ends8P0432
- Filtering chat spam by what is still on screen8P0391
- Theater seats and a waiting line8P0146
- Runoff with ranked ballots8P0148
2. Adding and removing #18–24
append adds one item at the end, and extend adds several.remove(x) deletes only the first x it finds.
If you delete from a list while a for loop is going over it, elements get skipped, so collect only the ones you want to keep into a new list.
Syntax refresher: Python List append(), extend() and insert() · Python: Remove Duplicates from a List
3. Slicing, reversing, swapping #25–38
a[1:4] is index 1 through index 3 (the end index is left out).a[::-1] gives you a new reversed list, and a.reverse() reverses the list itself.
Swapping two elements takes one line: a[i], a[j] = a[j], a[i].
Syntax refresher: Python Slicing: [start:stop:step] Explained
- Showing one page of a board8P0284
- Swapping places in pairs8P0312
- Reverse the word order8P0028
- Rotate a list right8P0032
- Shortest to the front, tallest to the back8P0317
- Best window average8P0106
- Folding a paper strip8P0354
- A function that reverses ranges8P0062
- Who moved after the seat swaps8P0176
- Tidying ranges of a warehouse shelf8P0177
- Copy, cut and paste8P0298
- Slice views that share cells, and copies8P0439
- Flipping through class-photo lineups8P0208
- Reversing bracelet beads across the clasp8P0408
4. Sorting #39–43
a.sort() changes a and returns None.
So if you write a = a.sort(), you lose a.
To keep the original, use sorted(a).
Set the order with key=, and sort in descending order with reverse=True.
Syntax refresher: Python sort() vs sorted(): Sort a List
5. Copying #44–49
b = a only puts one more name on the same list, so changing b changes a too.
To work on a separate one, use a.copy() or a[:].
If the list has lists inside it, these copy only the outer layer, so you need copy.deepcopy.
Syntax refresher: Python Slicing: [start:stop:step] Explained
6. 2D lists #50–58
[[0] * m] * n points to the same row n times, so changing one element changes every row.
Make each row separately with [[0] * m for _ in range(n)].
When you read a value, the order is grid[row][col].
Syntax refresher: Python for Loop · Python range(): Start, Stop and Step
- Row and column sums8P0033
- Flip and filter a grid1P0083
- Table of shared free days8P0234
- Taking every few diagonal stripes of a patchwork8P0335
- Picking the restaurant that disappoints no one much8P0334
- Finding a tile pattern, turned8P0426
- Pieces nobody attacks8P0299
- Fast rectangle sums on a grid8P0092
- Taking marbles without copying8P0339
Good to know
- The usual way to read one line of input into a list is
a = list(map(int, input().split())). - To print a list on one line, use
print(*a)or" ".join(map(str, a)).
See also
- 100 Python Practice Problems for Beginners — 100 problems
- 100 Coding Challenges for Beginners — 100 problems
- Learn Python — lessons with explanations and examples
- Python reference
- Python error messages
- Other problem sets