> For the complete documentation index, see [llms.txt](https://george-jen.gitbook.io/data-science-and-apache-spark/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://george-jen.gitbook.io/data-science-and-apache-spark/list-seq.md).

# List/Seq

### List

The Scala List is a sequence of elements, implemented as a linked list. Lists support efficient addition of new elements at the front.

Unlike Python list, which is mutable; Scala List is immutable

```
//create a list from known elements
val a: List[Int] = List(1, 2, 3, 4)
//a: List[Int] = List(1, 2, 3, 4)

//create an empty list
val b:List[Int]=List[Int]()
//Check if it is empty
println(b==Nil)
//add element into it
val c:List[Int]=1::2::3::4::b
//concat 2 list
val d:List[Int]=a++c

/*
true
b: List[Int] = List()
c: List[Int] = List(1, 2, 3, 4)
d: List[Int] = List(1, 2, 3, 4, 1, 2, 3, 4)
*/
```

### Seq

A base trait for sequences. Sequences are special cases of iterable collections of class Iterable. List is the implementation of Seq. &#x20;

Similar to List, but some difference when operating such as adding elements to existing Seq

```
//Create a Seq if you have elements
val s:Seq[Int]=Seq(1,2,3,4)
//s: Seq[Int] = List(1, 2, 3, 4)
//Create an empty Seq
val t:Seq[Int]=Seq[Int]()
//t: Seq[Int] = List()
t==Nil
//res5: Boolean = true
//Add elements to Seq, prepend
val u:Seq[Int]=t:+1:+2:+3:+4
//u: Seq[Int] = List(1, 2, 3, 4)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://george-jen.gitbook.io/data-science-and-apache-spark/list-seq.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
