prefix, suffix, and dropFirst
Take portions of a collection declaratively.
prefix, suffix, and dropFirst is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
prefix(n)
prefix(n) returns a slice of the first n elements. It is safe even if n is larger than the array.
let arr = [1, 2, 3, 4, 5]
print(Array(arr.prefix(3)))
print(Array(arr.prefix(99)))suffix(n)
suffix(n) returns a slice of the last n elements, also clamped safely.
let arr = [1, 2, 3, 4, 5]
print(Array(arr.suffix(2)))
print(Array(arr.suffix(0)))dropFirst
dropFirst() drops the first element; dropFirst(n) drops the first n.
let arr = [10, 20, 30, 40]
print(Array(arr.dropFirst()))
print(Array(arr.dropFirst(2)))dropLast
dropLast() removes from the end. Like the others, it returns a slice.
let arr = [10, 20, 30, 40]
print(Array(arr.dropLast()))
print(Array(arr.dropLast(3)))All Return Slices
Every one of these methods returns an ArraySlice, not a new array. Convert when you need a concrete array.
let arr = [1, 2, 3, 4]
let s = arr.prefix(2)
print(type(of: s))Safe With Large n
Unlike subscripting with an out-of-range range, these methods never crash. Asking for more than exists just returns what is available.
let arr = [1, 2]
print(Array(arr.prefix(10)))
print(Array(arr.dropFirst(10)))prefix(while:)
prefix(while:) takes elements from the start as long as a condition holds, then stops.
let arr = [2, 4, 6, 7, 8]
let evensAtStart = arr.prefix(while: { $0 % 2 == 0 })
print(Array(evensAtStart))drop(while:)
drop(while:) skips leading elements while a condition holds, returning the rest.
let arr = [2, 4, 6, 7, 8]
let rest = arr.drop(while: { $0 % 2 == 0 })
print(Array(rest))Combining Methods
Chain these to express slicing concisely, for example everything except the first and last element.
let arr = [10, 20, 30, 40, 50]
let middle = arr.dropFirst().dropLast()
print(Array(middle))Peek vs Pop Pattern
Use first to peek and dropFirst() to get the remainder, a common functional pattern for processing lists.
let queue = [100, 200, 300]
if let head = queue.first {
let tail = Array(queue.dropFirst())
print("head:", head, "tail:", tail)
}Readable Intent
These methods communicate intent better than manual ranges. arr.suffix(3) is clearer than arr[(arr.count-3)...].
let arr = [1, 2, 3, 4, 5, 6]
print(Array(arr.suffix(3)))Quick Check
Test prefix and drop behavior.
Recap
prefix(n) and suffix(n) take from the ends; dropFirst and dropLast remove from the ends. All return slices and are safe with oversized counts. prefix(while:) and drop(while:) use predicates.
let arr = [1, 2, 3, 4, 5]
print(Array(arr.prefix(2)), Array(arr.suffix(2)), Array(arr.dropFirst()))Frequently asked questions
Is the “prefix, suffix, and dropFirst” lesson free?
Yes — the full text of “prefix, suffix, and dropFirst” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “prefix, suffix, and dropFirst”?
Take portions of a collection declaratively. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “prefix, suffix, and dropFirst” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Creating Slices with Ranges
- ArraySlice Index Semantics
- Converting Slices Back to Arrays
- prefix, suffix, and dropFirst