What is the list.tail method in Scala?

The list.tail function in Scala returns the reference of all the elements in the list except the first element.

Figure 1 shows a visual representation of the list.tail function.

Figure 1: Visual representation of the list.tail function

Syntax

list_name.tail
// where the list_name is the name of the list

Parameters

This function does not require any parameters.

Return value

The list.tail function returns the reference of all the elements in the list except the first element.

  • It does not remove that element from the list.
  • If the list is empty, then it throws an error.

Code

The following code shows how to use the list.tail function in Scala.

object Main extends App {
var list = List(1,3,4,2,0)
//list = 1->3->4->2->0
println("Following are the elements in List before using list.tail: " + list);
println("The elements on the tail of list: "+list.tail);
println("Following are the elements in list after using list.tail: " + list);
}
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources