

{"id":13799,"date":"2018-05-04T12:25:13","date_gmt":"2018-05-04T12:25:13","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=13799"},"modified":"2021-12-04T10:15:46","modified_gmt":"2021-12-04T04:45:46","slug":"scala-list","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/scala-list\/","title":{"rendered":"Learn Scala List with Different Methods and Examples"},"content":{"rendered":"<p>In Scala, we have 6 kinds of collections. These are lists, sets, maps, <strong><a href=\"https:\/\/data-flair.training\/blogs\/tuples-in-scala-introduction\/\">tuples<\/a><\/strong>, options, and iterators. In this tutorial on Scala list, we will focus on Lists. We will learn to declare a Scala list, Creating Uniform List, Methods on a Scala list, Operations on a Scala list, Reversing a List in Scala Collections, Defining List using:: Nil, Tabulating a Function, Concatenating Scala list.<\/p>\n<p>So, let&#8217;s start learning Scala Lists.<\/p>\n<h3>Introduction of List in Scala Collections<\/h3>\n<p>A collection holds things. This can be strict or lazy(don\u2019t consume memory until accessed). This can also be mutable(reassignable) or immutable. In this tutorial, we will discuss Lists. Next, we\u2019ll look at Sets. Let\u2019s begin.<\/p>\n<p>A list of Scala Collections is much like a<strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-array\/\"> Scala array<\/a><\/strong>. Except that, it is immutable and represents a linked list. An Array, on the other hand, is flat and mutable. Here\u2019s an Array:<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var a=Array(1,2,'a')\r\na: Array[Int] = Array(1, 2, 97)\r\nscala&gt; a(2)\r\nres3: Int = 97\r\nscala&gt; a(2)=98\r\nscala&gt; a\r\nres5: Array[Int] = Array(1, 2, 98)<\/pre>\n<h3>Declaring a Scala List<\/h3>\n<p>Now, here\u2019s a Scala list to compare that to:<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var a=List(1,2,'a')\r\na: List[Int] = List(1, 2, 97)\r\nscala&gt; a(2)\r\nres8: Int = 97\r\nscala&gt; a(3)=98\r\n&lt;console&gt;:13: error: value update is not a member of List[Int]\r\n      a(3)=98\r\n      ^\r\nscala&gt; a=List(1,2,\"Ayushi\",true)\r\na: List[Any] = List(1, 2, Ayushi, true)<\/pre>\n<p>While declaring a Scala list, we can also denote the type of elements it will hold. If it\u2019ll hold more than one kind, we use \u2018Any\u2019. Here are few examples.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; val a:List[Int]=List(1,3,2)\r\na: List[Int] = List(1, 3, 2)\r\nscala&gt; val b:List[Char]=List('a','c','b')\r\nb: List[Char] = List(a, c, b)\r\nscala&gt; val c:List[String]=List(\"Ayushi\",\"Megha\")\r\nc: List[String] = List(Ayushi, Megha)\r\nscala&gt; val d:List[Nothing]=List()\r\nd: List[Nothing] = List()\r\nscala&gt; val e:List[List[Int]]=List(List(1,2,3),List(4,5,6),List(7,8,9))\r\ne: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6), List(7, 8, 9))<\/pre>\n<h3>Defining Scala List using :: and Nil<\/h3>\n<p>Think of Nil as an empty list, and :: as cons. If we had to define the above lists this way, we\u2019d:<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; val a=1::(2::(\"Ayushi\"::(true::Nil)))\r\na: List[Any] = List(1, 2, Ayushi, true)\r\nscala&gt; val a=1::2::3::Nil\r\na: List[Int] = List(1, 2, 3)\r\nscala&gt; val b='a'::('c'::('b'::Nil))\r\nb: List[Char] = List(a, c, b)\r\nscala&gt; val c=\"Ayushi\"::(\"Megha\"::Nil)\r\nc: List[String] = List(Ayushi, Megha)\r\nscala&gt; val d=Nil\r\nd: scala.collection.immutable.Nil.type = List()\r\nscala&gt; val  e=(1::(2::(3::Nil)))::(4::(5::(6::Nil)))::(7::(8::(9::Nil)))::Nil\r\ne: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6), List(7, 8, 9))<\/pre>\n<h3>Operations on Lists in Scala<\/h3>\n<p>We can carry out three operations on a Scala list to get a result:<\/p>\n<h4>a. head<\/h4>\n<p>This returns the first element of a Scala list.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; e.head\r\nres12: List[Int] = List(1, 2, 3)\r\nscala&gt; b.head\r\nres13: Char = a<\/pre>\n<h4>b. tail<\/h4>\n<p>This returns the last element of a Scala list.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; c.tail\r\nres14: List[String] = List(Megha)<\/pre>\n<h4>c. isEmpty<\/h4>\n<p>If the list is empty, this returns a Boolean true; otherwise, false.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; d.isEmpty\r\nres15: Boolean = true\r\nscala&gt; a.isEmpty\r\nres16: Boolean = false<\/pre>\n<h3>Concatenating Lists in Scala<\/h3>\n<p>We can join or concatenate two Scala lists in one of three ways. Let\u2019s take two lists for this:<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; val a=List(1,2,3)\r\na: List[Int] = List(1, 2, 3)\r\nscala&gt; val b=List(4,5,6)\r\nb: List[Int] = List(4, 5, 6)<\/pre>\n<h4>a. The ::: Operator<\/h4>\n<pre class=\"EnlighterJSRAW\">scala&gt; a:::b\r\nres0: List[Int] = List(1, 2, 3, 4, 5, 6)<\/pre>\n<h4>b. The List.:::() Method<\/h4>\n<p>We can call the :::() method on the first list.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.:::(b)\r\nres2: List[Int] = List(4, 5, 6, 1, 2, 3)\r\nscala&gt; a\r\nres3: List[Int] = List(1, 2, 3)<\/pre>\n<h4>c. The List.concat() Method<\/h4>\n<p>We can also call the concat() method on List in Scala Collections. We pass both the lists as arguments.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; List.concat(a,b)\r\nres5: List[Int] = List(1, 2, 3, 4, 5, 6)<\/pre>\n<p><strong>Read: <a href=\"https:\/\/data-flair.training\/blogs\/scala-operator\/\">Scala Operators with Syntax and Examples <\/a><\/strong><\/p>\n<h3>Creating Uniform Lists in Scala<\/h3>\n<p>The method List.fill() creates a list and fills it with zero or more copies of an element.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; val f=List.fill(7)(1)\r\nf: List[Int] = List(1, 1, 1, 1, 1, 1, 1)<\/pre>\n<p>This fills the list with seven instances of the integer 1.<\/p>\n<h3>Tabulating a Function<\/h3>\n<p>Using the List.tabulate() method with a function, we can tabulate a Scala list. The first argument specifies the dimensions; the second describes the elements(computed from a function).<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; val g=List.tabulate(7)(n=&gt;n*2)\r\ng: List[Int] = List(0, 2, 4, 6, 8, 10, 12)<\/pre>\n<p>We can also pass more than one size argument:<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; val h=List.tabulate(3,7)(_*_)\r\nh: List[List[Int]] = List(List(0, 0, 0, 0, 0, 0, 0), List(0, 1, 2, 3, 4, 5, 6), List(0, 2, 4, 6, 8, 10, 12))<\/pre>\n<p>This made a Scala List holding three Lists of lengths 7 each.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; val i=List.tabulate(7,5)(_*_)\r\ni: List[List[Int]] = List(List(0, 0, 0, 0, 0), List(0, 1, 2, 3, 4), List(0, 2, 4, 6, 8), List(0, 3, 6, 9, 12), List(0, 4, 8, 12, 16), List(0, 5, 10, 15, 20), List(0, 6, 12, 18, 24))<\/pre>\n<p>You can see that this creates a List holding 7 Lists each holding 5 elements. These are tables of integers from 0 up.<br \/>\n<strong>Read: <a href=\"https:\/\/data-flair.training\/blogs\/scala-functions\/\">Scala Functions with Syntax and Examples <\/a><\/strong><\/p>\n<h3>Reversing a Scala List<\/h3>\n<p>This reverses the order of elements in a list using the List.reverse method.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a\r\nres6: List[Int] = List(1, 2, 3)\r\nscala&gt; a.reverse\r\nres7: List[Int] = List(3, 2, 1)\r\nscala&gt; a\r\nres8: List[Int] = List(1, 2, 3)<\/pre>\n<h3>Methods on a List in Scala<\/h3>\n<p>We can call the following methods on a Scala List: (Note that these don\u2019t modify the Lists)<\/p>\n<h4>a. def +(elem: A): List[A]<\/h4>\n<p>This postpends an element to the Scala list.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.+(\"2\")\r\nres86: String = List(1, 2, 3)2<\/pre>\n<h4>b. def :::(prefix: List[A]): List[A]<\/h4>\n<p>This joins the List in the argument to the other List.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.:::(List(7,8,9))\r\nres16: List[Int] = List(7, 8, 9, 1, 2, 3)<\/pre>\n<h4>c. def ::(x: A): List[A]<\/h4>\n<p>This adds an element to a List\u2019s beginning.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.::(\"2\")\r\nres17: List[Any] = List(2, 1, 2, 3)<\/pre>\n<h4>d. def addString(b: StringBuilder): StringBuilder<\/h4>\n<p>This appends all elements of a Scala list to a String Builder.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var b=new StringBuilder()\r\nb: StringBuilder =\r\nscala&gt; a.addString(b)\r\nres19: StringBuilder = 123\r\nscala&gt; a\r\nres20: List[Int] = List(1, 2, 3)\r\nscala&gt; b\r\nres21: StringBuilder = 123<\/pre>\n<h4>e. def addString(b: StringBuilder, sep: String): StringBuilder<\/h4>\n<p>This does the same thing, except with a separator between the elements.<br \/>\nLet\u2019s first reset b to an empty string.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; b=new StringBuilder()\r\nb: StringBuilder =\r\nscala&gt; a.addString(b,\"*\")\r\nres23: StringBuilder = 1*2*3<\/pre>\n<p><strong>Read: <a href=\"https:\/\/data-flair.training\/blogs\/scala-data-types\/\">Scala Data Types <\/a><\/strong><\/p>\n<h4>f. def apply(n: Int): A<\/h4>\n<p>This selects an element in the Scala List by its index.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.apply(2)\r\nres24: Int = 3<\/pre>\n<h4>g. def contains(elem: Any): Boolean<\/h4>\n<p>If the list contains a certain element, this returns true; otherwise, false.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.contains(2)\r\nres25: Boolean = true\r\nscala&gt; a.contains(4)\r\nres26: Boolean = false<\/pre>\n<h4>h. def copyToArray(xs: Array[A], start: Int, len: Int): Unit<\/h4>\n<p>This copies the elements of a List to an Array. Start decides where to write, and len decides the length of elements to copy.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var arr=Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)\r\narr: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)\r\nscala&gt;  a.copyToArray(arr,2,3)\r\nscala&gt; arr\r\nres42: Array[Int] = Array(0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)<\/pre>\n<p>Let\u2019s try changing the length to 2.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var arr=Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)\r\narr: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)\r\nscala&gt; a.copyToArray(arr,2,2)\r\nscala&gt; arr\r\nres44: Array[Int] = Array(0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)<\/pre>\n<h4><strong>i. def distinct: List[A]<\/strong><\/h4>\n<p>Let\u2019s take a new Scala List for this.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var j=List(1,1,4,1,3,2,1)\r\nj: List[Int] = List(1, 1, 4, 1, 3, 2, 1)<\/pre>\n<p>Distinct returns a new List without duplicates.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; j.distinct\r\nres46: List[Int] = List(1, 4, 3, 2)<\/pre>\n<h4>j. def drop(n: Int): List[A]<\/h4>\n<p>This returns all elements except the first n.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; j.drop(2)\r\nres47: List[Int] = List(4, 1, 3, 2, 1)<\/pre>\n<h4>k. def dropRight(n: Int): List[A]<\/h4>\n<p>This returns all elements except the last n.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; j.dropRight(2)\r\nres48: List[Int] = List(1, 1, 4, 1, 3)<\/pre>\n<p><strong>Read <a href=\"https:\/\/data-flair.training\/blogs\/scala-string\/\">Scala String: Creating String, Concatenation, String Length<\/a><\/strong><\/p>\n<h4>l. def dropWhile(p: (A) =&gt; Boolean): List[A]<\/h4>\n<p>This drops the longest prefix of elements that satisfy the predicate.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; j.dropWhile(x=&gt;{x%2!=0})\r\nres52: List[Int] = List(4, 1, 3, 2, 1)<\/pre>\n<p>This dropped the elements 1 and 1. Then it stopped at 4 because it doesn\u2019t satisfy the predicate.<\/p>\n<h4>m. def equals(that: Any): Boolean<\/h4>\n<p>This compares two sequences.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.equals(b)\r\nres53: Boolean = false\r\nscala&gt; a.equals(List(1,2,3))\r\nres55: Boolean = true\r\nn. def exists(p: (A) =&gt; Boolean): Boolean<\/pre>\n<p>This returns true if a predicate holds true for any value in the Scala\u00a0 List.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.exists(x=&gt;{x%2!=0})\r\nres56: Boolean = true\r\no. def filter(p: (A) =&gt; Boolean): List[A]<\/pre>\n<p>This returns all such values.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.filter(x=&gt;{x%2!=0})\r\nres57: List[Int] = List(1, 3)\r\n<\/pre>\n<h4>p. def forall (p: (a) =&gt; Boolean) : Boolean<\/h4>\n<p>This returns true if the predicate holds true for all values in the List.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.forall(x=&gt;{x%2!=0})\r\nres58: Boolean = false<\/pre>\n<h4>q. def indexOf(elem: A, from: Int): Int<\/h4>\n<p>This returns the index of the first occurrence of the element in the List.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.indexOf(2)\r\nres61: Int = 1<\/pre>\n<h4>r. def init: List[A]<\/h4>\n<p>This returns all elements except the last.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.init\r\nres62: List[Int] = List(1, 2)<\/pre>\n<h4>s. def iterator: Iterator[A]<\/h4>\n<p>This creates an iterator over the Scala List.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.iterator\r\nres63: Iterator[Int] = non-empty iterator<\/pre>\n<h4>t. def lastIndexOf(elem: A, end: Int): Int<\/h4>\n<p>This returns the index of the last occurrence of an element, before or at given index.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; List(1,2,1,2,1).lastIndexOf(2)\r\nres64: Int = 3<\/pre>\n<h4>u. def length: Int<\/h4>\n<p>This returns a List\u2019s length.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.length\r\nres65: Int = 3<\/pre>\n<h4>v. def map[B](f: (A) =&gt; B): List[B]<\/h4>\n<p>This applies the function to every element in the list.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.map(x=&gt;x*x)\r\nres66: List[Int] = List(1, 4, 9)<\/pre>\n<h4>w. def max: A<\/h4>\n<p>This returns the highest element.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.max\r\nres67: Int = 3<\/pre>\n<h4>x. def min: A<\/h4>\n<p>This returns the lowest element.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.min\r\nres68: Int = 1<\/pre>\n<h4>y. def mkString: String<\/h4>\n<p>This displays all elements of a list in a String.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.mkString\r\nres69: String = 123<\/pre>\n<h4>z. def mkString(sep: String): String<\/h4>\n<p>This lets us define a separator for mkString.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.mkString(\"*\")\r\nres71: String = 1*2*3<\/pre>\n<h4>aa. def reverse: List[A]<\/h4>\n<p>This reverses a List of Scala Collections.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.reverse\r\nres72: List[Int] = List(3, 2, 1)<\/pre>\n<h4>ab. def sum: A<\/h4>\n<p>This returns the sum of all elements.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.sum\r\nres74: Int = 6<\/pre>\n<h4>ac. def take(n: Int): List[A]<\/h4>\n<p>This returns the first n elements.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.take(2)\r\nres75: List[Int] = List(1, 2)<\/pre>\n<h4>ad. def takeRight(n: Int): List[A]<\/h4>\n<p>This returns the last n elements.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.takeRight(2)\r\nres76: List[Int] = List(2, 3)<\/pre>\n<h4>ae. def toArray: Array[A]<\/h4>\n<p>This returns an Array from a List.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.toArray\r\nres78: Array[Int] = Array(1, 2, 3)<\/pre>\n<h4>af. def toBuffer[B &gt;: A]: Buffer[B]<\/h4>\n<p>This returns a Buffer from a Scala List.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.toBuffer\r\nres79: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3)<\/pre>\n<h4>ag. def toSeq: Seq[A]<\/h4>\n<p>This returns a Seq from a List.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.toSeq\r\nres81: scala.collection.immutable.Seq[Int] = List(1, 2, 3)<\/pre>\n<h4>ah. def toSet[B &gt;: A]: Set[B]<\/h4>\n<p>This returns a Set from a List.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.toSet\r\nres82: scala.collection.immutable.Set[Int] = Set(1, 2, 3)<\/pre>\n<h4>ai. def toString(): String<\/h4>\n<p>This returns a String from a List.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.toString\r\nres83: String = List(1, 2, 3)<\/pre>\n<p>This was all about Scala List. Hope you like our explanation.<\/p>\n<h3>Conclusion<\/h3>\n<p>This is all about Scala List. Stay tuned for tutorials on other kinds of collections like Sets and Maps. Furthermore, if you have any query, feel free to ask in the comment section.<br \/>\n<a href=\"https:\/\/www.scala-lang.org\/api\/2.12.3\/scala\/collection\/immutable\/List.html\">For reference<\/a><span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1957,&quot;href&quot;:&quot;https:\\\/\\\/www.scala-lang.org\\\/api\\\/2.12.3\\\/scala\\\/collection\\\/immutable\\\/List.html&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20250321052511\\\/https:\\\/\\\/www.scala-lang.org\\\/api\\\/2.12.3\\\/scala\\\/collection\\\/immutable\\\/List.html&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-10 13:33:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-30 04:49:30&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-02 16:16:47&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-06 02:42:47&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-10 06:56:24&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-17 08:34:38&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-14 08:52:08&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-22 05:01:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-25 20:02:09&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-08 06:26:45&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-23 18:42:02&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-29 13:10:41&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-05 10:05:31&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-17 06:56:53&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-30 07:38:39&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-30 07:38:39&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Scala, we have 6 kinds of collections. These are lists, sets, maps, tuples, options, and iterators. In this tutorial on Scala list, we will focus on Lists. We will learn to declare a&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":34770,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[3160,3633,8694,9281],"class_list":["post-13799","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scala","tag-creating-uniform-list","tag-declare-a-scala-collection-list","tag-methods-on-a-scala-collections-list","tag-operations-on-a-scala-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Learn Scala List with Different Methods and Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"Scala List: Learn to declare Scala list, Creating Uniform List, Operating list, Concatenating Lists, methods on a Scala list, Operations on Scala lists\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/scala-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Learn Scala List with Different Methods and Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Scala List: Learn to declare Scala list, Creating Uniform List, Operating list, Concatenating Lists, methods on a Scala list, Operations on Scala lists\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/scala-list\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-05-04T12:25:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-04T04:45:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Scala-Collections-Lists-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Learn Scala List with Different Methods and Examples - DataFlair","description":"Scala List: Learn to declare Scala list, Creating Uniform List, Operating list, Concatenating Lists, methods on a Scala list, Operations on Scala lists","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/scala-list\/","og_locale":"en_US","og_type":"article","og_title":"Learn Scala List with Different Methods and Examples - DataFlair","og_description":"Scala List: Learn to declare Scala list, Creating Uniform List, Operating list, Concatenating Lists, methods on a Scala list, Operations on Scala lists","og_url":"https:\/\/data-flair.training\/blogs\/scala-list\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-05-04T12:25:13+00:00","article_modified_time":"2021-12-04T04:45:46+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Scala-Collections-Lists-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/scala-list\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/scala-list\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Learn Scala List with Different Methods and Examples","datePublished":"2018-05-04T12:25:13+00:00","dateModified":"2021-12-04T04:45:46+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/scala-list\/"},"wordCount":1144,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/scala-list\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Scala-Collections-Lists-01.jpg","keywords":["Creating Uniform List","declare a Scala collection list","methods on a Scala Collections list","Operations on a Scala list"],"articleSection":["Scala Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/scala-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/scala-list\/","url":"https:\/\/data-flair.training\/blogs\/scala-list\/","name":"Learn Scala List with Different Methods and Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/scala-list\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/scala-list\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Scala-Collections-Lists-01.jpg","datePublished":"2018-05-04T12:25:13+00:00","dateModified":"2021-12-04T04:45:46+00:00","description":"Scala List: Learn to declare Scala list, Creating Uniform List, Operating list, Concatenating Lists, methods on a Scala list, Operations on Scala lists","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/scala-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/scala-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/scala-list\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Scala-Collections-Lists-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/Scala-Collections-Lists-01.jpg","width":1200,"height":628,"caption":"Learn Scala List with Different Methods and Examples"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/scala-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Scala Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/scala\/"},{"@type":"ListItem","position":3,"name":"Learn Scala List with Different Methods and Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=13799"}],"version-history":[{"count":6,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13799\/revisions"}],"predecessor-version":[{"id":104774,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13799\/revisions\/104774"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/34770"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=13799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=13799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=13799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}