Swift Academy · Lesson

Iterating Collections with for-in

Looping over arrays, dictionaries and sets with enumerated and zip.

Lesson 3 of 412 steps

Iterating Collections with for-in is a free Swift Academy lesson on CoddyKit. This is lesson 3 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.

Welcome

The `for-in` loop works with any `Sequence` — arrays, dictionaries, sets, strings, and your own custom types. Let's see the most useful patterns.

Iterating an Array

```swift let fruits = ["apple", "banana", "cherry"] for fruit in fruits { print(fruit) } // apple, banana, cherry ``` Each element is bound to `fruit` for one iteration.

enumerated() — Index and Value

When you need both the index and value, use `.enumerated()`: ```swift for (index, fruit) in fruits.enumerated() { print("\(index): \(fruit)") } // 0: apple // 1: banana // 2: cherry ``` `enumerated()` returns a sequence of `(offset, element)` tuples.

Iterating a Dictionary

Dictionary iteration yields `(key, value)` tuples in unspecified order: ```swift let scores = ["Alice": 95, "Bob": 87] for (name, score) in scores { print("\(name): \(score)") } ``` Sort the keys first if you need a deterministic order.

Iterating a Set

Sets iterate in an unspecified order: ```swift let colors: Set = ["red", "green", "blue"] for color in colors.sorted() { print(color) } ``` Call `.sorted()` when you need alphabetical or ascending order.

zip() — Parallel Iteration

Iterate two sequences in parallel with `zip()`: ```swift let names = ["Alice", "Bob", "Carol"] let scores = [95, 87, 92] for (name, score) in zip(names, scores) { print("\(name): \(score)") } ``` `zip` stops at the shorter sequence.

Filtering While Iterating: where Clause

Add a `where` clause to skip elements inline: ```swift for n in 1...20 where n % 3 == 0 { print(n) // 3, 6, 9, 12, 15, 18 } ``` This is cleaner than putting an `if` inside the loop body.

Iterating with indices

Access the raw index range with `.indices`: ```swift var arr = [10, 20, 30] for i in arr.indices { arr[i] *= 2 } // arr is now [20, 40, 60] ``` Use this when you need to *mutate* elements in place.

Destructuring Tuples in for-in

Sequences of tuples can be destructured directly in the loop: ```swift let points = [(1, 2), (3, 4), (5, 6)] for (x, y) in points { print("x=\(x), y=\(y)") } ``` Clear and concise — no need for `.0` and `.1` subscripts.

Lazy Sequences in Loops

Prefix `.lazy` to defer computation and avoid intermediate arrays: ```swift let total = (1...1_000_000) .lazy .filter { $0 % 2 == 0 } .prefix(5) .reduce(0, +) print(total) // 30 ``` `.lazy` is powerful when chaining operations on large sequences.

Quick Check

Which method gives you both the index and value when iterating an array?

Recap

Key takeaways: • `for elem in collection` — basic iteration • `.enumerated()` — index + value pairs • `zip(a, b)` — parallel iteration of two sequences • `for x in seq where condition` — inline filter • `.indices` — use when mutating elements • `.lazy` — defer work on large sequences Next: break, continue and labeled statements.
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 “Iterating Collections with for-in” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Iterating Collections with for-in”, 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 “Iterating Collections with for-in”?

Looping over arrays, dictionaries and sets with enumerated and zip. 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 3 of 4.

How long does the “Iterating Collections with for-in” 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. for-in Loops with Ranges
  2. while and repeat-while Loops
  3. Iterating Collections with for-in
  4. break, continue and Labeled Statements
← Back to Swift Academy