Hi,
The yield keyword is used because the result of each for loop iteration is stored in a list (vector) or it collects the list of output and saves it in the vector. Below an example to see the code and its input:

The above is a for loop with yield function and you can see the below code to store the output in a vector form.

Hope it helps!
If you want to know more about Apache Spark Scala, It's highly recommended to go for Apache Spark certification course today.
Thanks!!