WolframLang: Get Parts of List

By Xah Lee. Date: . Last updated: .

Get Element by Position

Get Parts by Index

Part[expr, i]

🔸 SHORT SYNTAX: expr[[i]]

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

Part

Part[{a, b, c, {d1, d2}, e}, 2]
(* b *)

(* short syntax *)
{a, b, c, {d1, d2}, e}[[2]]
(* b *)

(* count from right *)
{a, b, c, {d1, d2}, e}[[-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] }

{a, b, c, {d1, d2}, e}[[{2,-1}]]

(* {b, e} *)
Part[expr, a, b, c]

🔸 SHORT SYNTAX: expr[[a, b, c]]

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

{a, b, c, {d1, d2}, e}[[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.

{a, b, c, {d1, d2}, e}[[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 Items by Range of Index

Take

take nth to mth item

Take

Drop

Return a list with nth to mth dropped

Drop

Other List Extraction 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

Get Elements by Function or Pattern

WolframLang List Operations