How to access list elements in Scala? Last Updated : 18 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In this articleIn this article, we will learn to access list elements in Scala. List in Scala is the most commonly used collection type, providing an ordered collection that allows accessing elements by index or using functional programming. How to Access List Elements in Scala?Below are the possible approaches to access list elements in Scala. Approach 1: Using for Loop In this approach, we are using a for loop to iterate through each element of the list. For each iteration, the loop variable elem represents one element of the list. Inside the loop, we print each element using the println function, resulting in printing all elements of the list to the console.In the below example, for Loop is used to access list elements in Scala. Scala object GFG { def main(args: Array[String]): Unit = { val list = List("Geeks", "For", "geeks", "is", "a", "fabulous", "portal") for (elem <- list) { println(elem) } } } Output: Approach 2: Using 'forEach' methodIn this approach, we are using the foreach method on the list to iterate through each element. The println function is applied to each element, which automatically prints each element of the list to the console. This method provides a simple way to iterate over the list and perform an action on each element without explicitly using a loop.In the below example, forEach method is used to access list elements in Scala. Scala object GFG { def main(args: Array[String]): Unit = { val list = List("Geeks", "For", "geeks", "is", "a", "fabulous", "portal") list.foreach(println) } } Output: Approach 3: Using pattern matching and recursionIn this approach, we define a recursive function printList that uses pattern matching to handle the list. If the list is empty (Nil), the function does nothing. Otherwise, it matches the list into a head element (head) and the remaining elements (tail). It then prints the head element and recursively calls itself with the tail, effectively printing all elements of the list. Finally, in the main method, we call printList with the given list to print all its elements.In the below example, pattern matching and recursion is used to access list elements in Scala. Scala object GFG { def printList(list: List[String]): Unit = list match { case Nil => case head :: tail => println(head) printList(tail) } def main(args: Array[String]): Unit = { val list = List("Geeks", "For", "geeks", "is", "a", "fabulous", "portal") printList(list) } } Output: Comment More infoAdvertise with us Next Article How to access list elements in Scala? G gpancomputer Follow Improve Article Tags : Scala Similar Reads How to get the first element of List in Scala In Scala, list is defined under scala.collection.immutable package. A list is a collection of same type elements which contains immutable data. Let's see how to get the first element of given List in Scala.List in Scala contains many suitable methods to perform simple operations like head(), tail(), 1 min read How to skip only first element of List in Scala In Scala, list is defined under scala.collection.immutable package. A list is a collection of same type elements which contains immutable data. Let's see how to print the element of given List, by skipping the first item in Scala. List in Scala contains many suitable methods to perform simple operat 1 min read Find the last element of a list in scala In Scala, list is defined under scala.collection.immutable package. A list is a collection of same type elements which contains immutable data. we generally use last function to print last element of a list. Below are the examples to find the last element of a given list in Scala. Simply print last 2 min read How to reverse a list in scala In Scala, list is defined under scala.collection.immutable package. A list is a collection of same type elements which contains immutable data. We use reverse function to reverse a list in Scala. Below are the examples to reverse a list. Reverse a simple list scala // Scala program to reverse a simp 2 min read How to Add Element in an Immutable Seq in Scala? In this article, we will learn how to add elements in immutable seq in Scala. immutable sequences, such as List, cannot have elements added to them directly because they are designed to be immutable. However, you can create a new sequence with an additional element by using methods like :+ (for appe 2 min read Like