Question: I'm struggling to change it to use a for loop and still do the same thing.

The program is supposed to read a file with some flights and this specific part of the program needs to read the file using two different days that the user inputs then it needs to show how many passengers there are per flight and each day.

And how it's done now works but I'm trying to change it to use a for loop as I said before but doesn't work because I don't know how to do the same thing as map does but only in the `fun interval`.

```
fun interval(reservas: List<Reservas>, dayInferior: Int, daySuperior: Int) {
    val map = mapReservas(reservas)
    for(day in dayInferior..daySuperior) {
        map.forEach {
            val reservasNum = it.key.first
            val reservasDay = it.key.second
            val reservasCount = it.value.count()
            if (reservasDay == day) {
                 println("$reservasNum has $reservasCount passengers on day $day")
            }
        }
    }
    println()
    println("Press Enter")                                                              
    readLine()
}

fun mapReservas(reservas: List<Reservas>): Map<Pair<String, Int>, List<Reservas>> {
    val map = mutableMapOf<Pair<String, Int>, MutableList<Reservas>>()
    for (reserva in reservas) {
        val key = reserva.numFlight to reserva.day
        val list = map[key] ?: mutableListOf()
        list.add(reserva)
        map[key] = list
    }
    return map
}

```

Answer: 
All your code can be replaced only with one function.

```
fun interval(reservas: List<Reservas>, dayInferior: Int, daySuperior: Int) {
    reservas.groupBy { reserva -> reserva.day to reserva.numFlight }
        .filter { (key, _) -> key.first in dayInferior..daySuperior }
        .forEach { (key, reservas) ->
            val (reservasNum, reservasDay) = key
            val reservasCount = reservas.count()
            println("$reservasNum has $reservasCount passengers on day $reservasDay")
    }
    println()
    println("Press Enter")
    readLine()
}

```

Explaining:

1. As I undestand, at first you trying to group all your `Reservas` by `day` and `numFlight`. It can be done via one function `groupBy` where you pass pair of `day` and `numFlight`.
2. Filter all `Reservas` by day. It can be done by checking if day belongs to range `dayInferior..daySuperior` (operator `in`).
3. Print all reservas by using `forEach`.

Other things

1. [Destructing declarations](https://kotlinlang.org/docs/reference/multi-declarations.html)

```
val reservasNum = it.key.first
val reservasDay = it.key.second

same as 

val (reservasNum, reservasDa) = it.key

```
2. [Omitting one unused parameter in lamda](https://kotlinlang.org/docs/reference/multi-declarations.html#underscore-for-unused-variables-since-11):

```
.filter { (key, _) -> ... }

```
If you iterate with a for loop over the `Map` each element is a `Pair`. If you write `(pair, list)` you destructure each Pair which itself consists of a `Pair` and a `List`.

```
fun interval(reservas: List<Reservas>, dayInferior: Int, daySuperior: Int) {
    val map = mapReservas(reservas)

    for(day in dayInferior..daySuperior) {

        for((pair, list) in map) {
            val reservasNum = pair.first
            val reservasDay = pair.second
            val reservasCount = list.count()
            // ...
        }
    }
    // ...
}

```

Maybe this makes it more clear:

```
for(outerPair in map){
    val (innerPair, list) = outerPair
    val reservasNum = innerPair.first
    val reservasDay = innerPair.second
    val reservasCount = list.count()
    // ...
}

```

I left this function (`mapReservas`) untouched intentionally, because maybe you are using it somewhere else. But you can improve it right away by using [Type aliases](https://kotlinlang.org/docs/reference/type-aliases.html) (since Kotlin 1.1).

```
typealias FlightNum = String
typealias Day = Int

fun mapReservas(reservas: List<Reservas>): 
                  Map<Pair<FlightNum, Day>, List<Reservas>>  {
   // ...
}

```

As you can see the code becomes much more readable if you use the destructure syntax and Type aliases.