# 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: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
