Swift Academy · Lesson

prefix, suffix, and dropFirst

Take portions of a collection declaratively.

Lesson 4 of 413 steps

prefix, suffix, and dropFirst is a free Swift Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

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()))
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “prefix, suffix, and dropFirst” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “prefix, suffix, and dropFirst”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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

  1. Creating Slices with Ranges
  2. ArraySlice Index Semantics
  3. Converting Slices Back to Arrays
  4. prefix, suffix, and dropFirst
← Back to Swift Academy