Wolfram: List. Get Parts

By Xah Lee. Date: . Last updated: .

Get Item by Position

Get Item by Index

Get a Single Item by Index

Part[expr, i]

🔸 SHORT SYNTAX: expr[[i]]

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

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} *)

Get Multiple Items by Index

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} *)

Get Nested Item by Indexes

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 *)

Get a range of Items by Index Range

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

(* basic examples of Take *)

(* First 3 *)
Take[{1, 2, 3, 4, 5, 6, 7, 8, 9}, 3]
(* {1, 2, 3} *)

(* last 3 *)
Take[{1, 2, 3, 4, 5, 6, 7, 8, 9},-3]
(* {7, 8, 9} *)

(* m to n *)
Take[{1, 2, 3, 4, 5, 6, 7, 8, 9}, {3,5}]
(* {3, 4, 5} *)
Drop

Return a list with nth to mth dropped

Other List Extraction by Index Functions

First

get first element.

Last

get last element.

Most

get all elements except last.

Rest

get all elements except first.

Wolfram. List Operations, and Loop, Iteration, Recursion