{ }codelper

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.

58 problemsLevel 1–5Solve in Python

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

  1. Largest number and its position8P0005
  2. Is it on the list?8P0059
  3. Score statistics8P0003
  4. Travel time on a one-way bus route8P0224
  5. Masking banned words in chat8P0194
  6. Stamping a grid8P0061
  7. A tournament where winning tires you8P0180
  8. A frog on stepping stones8P0193
  9. What the recipes are missing8P0237
  10. Finding a melody fragment in a song8P0437
  11. Spiral fill8P0128
  12. Vertical survey histogram8P0150
  13. Shaping a list by passing rule functions8P0239
  14. Display board cells counted from both ends8P0432
  15. Filtering chat spam by what is still on screen8P0391
  16. Theater seats and a waiting line8P0146
  17. 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

  1. Numbers of the students who retake8P0374
  2. Remove every match8P0058
  3. Remove duplicates8P0035
  4. Edit a playlist8P0110
  5. Slime Hunting Log8P0390
  6. Card row clean-up commands8P0244
  7. Parcel trucks that leave when full8P0247

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

  1. Showing one page of a board8P0284
  2. Swapping places in pairs8P0312
  3. Reverse the word order8P0028
  4. Rotate a list right8P0032
  5. Shortest to the front, tallest to the back8P0317
  6. Best window average8P0106
  7. Folding a paper strip8P0354
  8. A function that reverses ranges8P0062
  9. Who moved after the seat swaps8P0176
  10. Tidying ranges of a warehouse shelf8P0177
  11. Copy, cut and paste8P0298
  12. Slice views that share cells, and copies8P0439
  13. Flipping through class-photo lineups8P0208
  14. 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

  1. Sort the numbers8P0036
  2. Sorting sign letters8P0253
  3. Number order and dictionary order8P0320
  4. Union, intersection, difference8P0099
  5. Building a back-of-the-book index8P0302

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

  1. Edit only the copy8P0060
  2. Save slots for a list8P0107
  3. A function that takes any number of judge scores8P0333
  4. Lists inside lists and shallow copies8P0406
  5. Used before a value was put in8P0360
  6. Identical parcels and parcels with the same contents8P0205

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

  1. Row and column sums8P0033
  2. Flip and filter a grid1P0083
  3. Table of shared free days8P0234
  4. Taking every few diagonal stripes of a patchwork8P0335
  5. Picking the restaurant that disappoints no one much8P0334
  6. Finding a tile pattern, turned8P0426
  7. Pieces nobody attacks8P0299
  8. Fast rectangle sums on a grid8P0092
  9. Taking marbles without copying8P0339

Good to know

See also