UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to use the zip() function to join two arrays

Swift version: 5.6

Paul Hudson    @twostraws   

The zip() function is designed to merge two sequences into a single sequence of tuples. For example, here is an array of wizards:

let wizards1 = ["Harry", "Ron", "Hermione"]

And here’s a matching array of the animals owned by those wizards:

let animals1 = ["Hedwig", "Scabbers", "Crookshanks"]

Using zip() we can combine them together:

let combined1 = zip(wizards1, animals1)

That will produce a single sequence combining the earlier two. To avoid doing extra work, Swift actually creates a special type called Zip2Sequence that stores both sequences internally – this is more efficient than doing the actual joining, but it does make the output harder to read if you’re using a playground. So, if you are using a playground you should wrap the output from zip() into a new array to make its output easier to read:

let combined2 = Array(zip(wizards1, animals1))

If you print combined you’ll see it contains this array:

[("Harry", "Hedwig"), ("Ron", "Scabbers"), ("Hermione", "Crookshanks")]

One of the helpful features of zip() is that if your two arrays differ in size it will automatically choose the shorter of the two. This avoids trying to read two arrays at the same time and accidentally going out of bounds when one is shorter.

For example, this code will print out the animals belonging to the first three wizards, but nothing for Draco because he doesn’t have a matching animal:

let wizards2 = ["Harry", "Ron", "Hermione", "Draco"]
let animals2 = ["Hedwig", "Scabbers", "Crookshanks"]

for (wizard, animal) in zip(wizards2, animals2) {
    print("\(wizard) has \(animal)")
}

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 8.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.2/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.