0Pricing
Swift Academy · Lesson

Iterating and Transforming Dictionaries

Using mapValues, filter, merge and grouping data with dictionaries.

Iterating and Transforming Dictionaries is a free Swift Academy lesson on CoddyKit — lesson 2 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.

Welcome

Dictionaries are full `Sequence` types. They support `for-in`, `map`, `filter`, `reduce`, and more — giving you a functional toolkit over keyed data.

for-in Iteration

```swift let prices = ["apple":1.5,"banana":0.8] for (fruit, price) in prices { print("\(fruit): $\(price)") } ``` Order is not guaranteed — sort `prices.sorted { $0.key < $1.key }` for determinism.

mapValues

```swift let scores = ["Alice":80,"Bob":90] let doubled = scores.mapValues { $0 * 2 } // ["Alice":160,"Bob":180] ``` `mapValues` keeps keys intact and transforms only the values.

filter on Dictionaries

```swift let d = ["a":1,"b":5,"c":3] let big = d.filter { $0.value > 2 } // ["b":5,"c":3] ```

Grouping with Dictionary(grouping:by:)

```swift let words = ["apple","ant","ball","banana"] let grouped = Dictionary(grouping: words) { $0.first! } // ["a":["apple","ant"],"b":["ball","banana"]] ``` An extremely useful pattern for categorising data.

reduce Into a Dictionary

```swift let nums = [1,2,3,4,5] let freq = nums.reduce(into: [Int:Int]()) { $0[$1, default:0] += 1 } // [1:1,2:1,3:1,4:1,5:1] ```

sorted Keys Iteration

```swift let d = ["c":3,"a":1,"b":2] for key in d.keys.sorted() { print("\(key): \(d[key]!)") } // a:1, b:2, c:3 ```

compactMapValues

```swift let raw = ["a":"1","b":"two","c":"3"] let nums = raw.compactMapValues { Int($0) } // ["a":1,"c":3] — "b" dropped ``` Transforms values and removes entries whose transform returns nil.

Reducing to a Single Value

```swift let sales = ["Q1":1000,"Q2":1200,"Q3":800] let total = sales.values.reduce(0, +) // 3000 let max = sales.values.max()! // 1200 ```

Dictionary from Two Arrays

```swift let keys = ["a","b","c"] let vals = [1, 2, 3] let d = Dictionary(uniqueKeysWithValues: zip(keys, vals)) // ["a":1,"b":2,"c":3] ```

Quick Check

Which method transforms dictionary values while keeping keys the same?

Recap

Key takeaways: • `for (k,v) in dict` — iterate; sort keys for determinism • `mapValues` — transform values, keep keys • `filter` — returns new dictionary of matching pairs • `Dictionary(grouping:by:)` — powerful grouping initialiser • `compactMapValues` — transform + drop nils Next: Set operations.

Frequently asked questions

Is the “Iterating and Transforming Dictionaries” lesson free?

Yes — the full text of “Iterating and Transforming Dictionaries” 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 “Iterating and Transforming Dictionaries”?

Using mapValues, filter, merge and grouping data with dictionaries. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Iterating and Transforming Dictionaries” 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. Dictionary CRUD and Subscript Access
  2. Iterating and Transforming Dictionaries
  3. Set Operations: Union, Intersection, Difference
  4. Choosing Between Array, Dictionary and Set
← Back to Swift Academy