Lesson 20 of 55
9 mins readPython List Functions: head, tail, take, drop & null
In Plain English
Haskell standard prelude provides essential functions to inspect, slice, and query lists efficiently.
Deep Dive: How It Works
Decomposition: head returns the first item; tail returns all remaining items; last returns the final item; init returns all except the last item.
Slicing: take n xs extracts the first n elements; drop n xs discards the first n elements.
Safety: null xs checks if a list is empty in O(1) time (prefer over length xs == 0).
Core Rules to Remember

take and drop: Slice lists cleanly: take 3 [1..10] gives [1, 2, 3].

null over length: null xs works instantly on infinite lists, whereas length forces full traversal.
Live Interactive Example
Hit Run Code to see it liveList Inspection and Slicing
Python 3.12
1
2
3
4
5
6
7
8
9
10
Output Console
Click "Run Code" to view the rendered output.
How it works: take 3 extracts first 3 items; drop 2 returns the rest.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Extract Middle Slice with take and drop
Take `3` items after dropping `2` from `[1..10]`: `take 3 (drop 2 [1..10])`.
Print `"Slice: "` followed by its `show` representation.
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.