# WolframLang: Get Parts of List

By Xah Lee. Date: . Last updated: .

There are many functions that extract sub-expression:

• `Extract` → get any arbitrarily nested element by Position or list of positions.
• `Part` → get top-level elements by index, or index range, or get value from Association (Hashtable, Map, Dictionary)
• `Select` → get elements by a filtering function.
• `Cases` → get elements by Pattern.

## Get Parts by Index

the `Part` function is the most versatile and most frequently used function to get part of a expression.

`Part[expr, i]`
(Short syntax: `expr[[i]]`)

ith part of expr. If i is negative, count from right. Part

```x = {a, b, c, {d1, d2}, e};
Part[x, 2] === b

(* short syntax *)
x[] === b

(* count from right *)
x[[-2]] === {d1, d2}```
`Part[expr, {a, b, c}]`
(Short syntax: `expr[[{a, b, c}]]`)

Same as `{Part[expr, a], Part[expr, b], Part[expr, c]}`

```x = {a, b, c, {d1, d2}, e};
x[[{2,-1}]] === {b, e}```
`Part[expr, a, b, c]`
(Short syntax: `expr[[a, b, c]]`)

same as `Part[Part[Part[expr, a], b], c]`

```x = {a, b, c, {d1, d2}, e};
x[[4,1]] === d1```
`Part[expr, m;;n]`
(Short syntax: `expr[[m;;n]]`)

(note: `a;;b` is short for `Span[a,b]`)

parts m through n. If m is omitted, default to beginning. If n is omitted, default to end.

```x = {a, b, c, {d1, d2}, e};
x[[2;;4]] === {b, c, {d1, d2}}```
• `expr[[m;;]]` → part m to end
• `expr[[;;n]]` → beginning to n
• `expr[[;;,j]]` → column j
• `expr[[m1;;n1,m2;;n2]]` → submatrix
```(* column 1 *)
{{a,b}, {c,d}}[[;;,1]] === {a, c}```

## Get Parts by Filter Function

`Select`
Get elements by a filter function Select
```Select[ {3,4,5,6}, EvenQ ] === {4, 6}

Select[ Range, PrimeQ ] === {2, 3, 5, 7, 11, 13, 17, 19}```
```Select[
{{3}, {0, 23}, {9}, {4,5,6}},
Function[ Length[#] === 2 ]
] === {{0, 23}}```

## Get Parts by Pattern

`Cases`
Get elements by Pattern Cases
```Cases[ {a, f, f}, f[_] ] === {f, f}

Cases[ {a, f, g, f}, _[_] ] === {f, g, f}

Cases[ {{2}, {4, 8}, {c}}, {_ , _} ] === {{4, 8}}```

## Other convenient functions by index

`First`
get first element. First
`Last`
get last element. Last
`Most`
get all elements except last. Most
`Rest`
get all elements except first. Rest
`Take`
take nth to mth item Take
`Drop`
Return a list with nth to mth dropped Drop