Iterate Through a Dictionary in Swift


In Swift, a dictionary is a data structure that can store information in key-value pairs. Now in this tutorial, you will learn how to iterate or loop through a dictionary in Swift programming.

By iterating a dictionary, you will be able to access the keys and values within the loop. This task can be done using both for loop and forEach loop. These are two well-known techniques also to perform the task.

Before I go forward, I want to let you know that Swift dictionaries are not ordered. So there is no guarantee that you are going to get the same order of the dictionary after iterating.

Iterating over a dictionary using the for-in loop

The easiest way to iterate through a dictionary is by using the Swift for-in loop.

A dictionary consists of keys and values. So to iterate over a dictionary it is required to take two variables, one is for the keys and the other for the values.

Below is the syntax for looping through a dictionary in Swift programming:

for (key,value) in DICTIONARY_LITERATE{
     print(" (key): (value) ")
}

Now see an example:

var studentName = [
    134: "George",
    143: "Hannah",
    187: "Jeremy"
]

for (key,value) in studentName {
    print("\(key): \(value)")
}

The output of the above program is:

134: George
187: Jeremy
143: Hannah

Well, the items may not be in the same order in your case if you run the program in your machine. Also, the order may be changed every time you run the program.

There is another way to perform this task with only one variable in the for-in loop. But, in this case, you have to pass the key and value property. Below is the example:

var studentName = [
    134: "George",
    143: "Hannah",
    187: "Jeremy"
]

for student in studentName {
    print("\(student.key): \(student.value)")
}

Iterating over a dictionary using the forEach loop (forEach method)

You can also use the Swift forEach method for looping over dictionaries. Here you will learn three different ways that can be helpful to iterate through a dictionary.

Here is a syntax for loping of dictionary using the forEach method:

dictionary_name.forEach{
     print("\($0.key): \($0.value)")
}

Below is the example:

var studentsID = [
    9: "Harold",
    13: "Jacqueline",
    19: "Austin"
]

studentsID.forEach {
    print("\($0.key): \($0.value)")
}

Output:

13: Jacqueline
19: Austin
9: Harold

In the above example, $0 is the shorthand argument. if we don’t set any explicit argument then in Swift, it will use $0 for the first shorthand argument, $1 for the second, $2 for the third, and so on…

In this way, you don’t have to take any variable for keys and values.

If we want, then we can also set the argument for the item by yourself like you can see in the example below:

var studentsID = [
    9: "Harold",
    13: "Jacqueline",
    19: "Austin"
]

studentsID.forEach { student in
    print("\(student.key): \(student.value)")
}

We can also use explicit arguments for both key and value. Below is the syntax:

DICTIONARY_NAME.forEach { key, value in
   print("\(key): \(value)")
}

Below is the code example of the above syntax:

var studentsID = [
    9: "Harold",
    13: "Jacqueline",
    19: "Austin"
]

studentsID.forEach { id, name in
    print("\(id): \(name)")
}

The above program also gives the same output as the previous code, but in a different way. Here you have set the arguments by yourself.


I hope you have understood how we can loop over dictionary items to iterate in Swift programming.