Python practice

Python list and array practice questions

15 problems, ordered from easiest. They run in your browser, and 15 are free.

Start with Collapse Consecutive Duplicates

The idea in a minute

A list holds values in order. Most array problems come down to walking through it, keeping a running answer, and handling the edge cases such as an empty list.

The problems begin with sums and maximums and move on to reversing, rotating and merging.

Example: Running total and maximum

nums = [4, 9, 2, 7]
total = 0
best = nums[0]
for n in nums:
    total += n
    best = max(best, n)
print(total, best, nums[::-1])

What it prints

22 9 [7, 2, 9, 4]

What to watch for

  • An empty list is the edge case that most solutions forget.
  • Copying a list with b = a does not copy it. Use a.copy() or a[:].
  • Negative indexes count from the end, so a[-1] is the last item.

Arrays problems

  1. Collapse Consecutive Duplicatesbeginner · Free
  2. Remove Duplicates Preserving Orderbeginner · Free
  3. Find Missing Numberbeginner · Free
  4. Product Except Selfbeginner · Free
  5. Best Time Buy Sellbeginner · Free
  6. Find All Duplicatesbeginner · Free
  7. Find Disappearedbeginner · Free
  8. Find Duplicate Numberbeginner · Free
  9. Forward-fill Nonebeginner · Free
  10. Smallest number from odd digitsbeginner · Free
  11. Negatives before non-negativesbeginner · Free
  12. Split records into batchesbeginner · Free
  13. Skip tasks that already ranbeginner · Free
  14. Wait times for retriesbeginner · Free
  15. Load only the new rowsbeginner · Free

Want a guided order across every topic? Open the practice sheets or the Python hub.