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:
![](https://www.edureka.co/community/?qa=blob&qa_blobid=8881571951587818503)
The above is a for loop with yield function and you can see the below code to store the output in a vector form.
![](https://www.edureka.co/community/?qa=blob&qa_blobid=7953134105339211041)
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!!