Wolfram: List. Delete Element

By Xah Lee. Date: . Last updated: .

Delete by Index or Index Range

These are useful when the expression is considered as a flat list.

Delete N Items from Beginning or End

Drop[ expr, n]
  • Delete n items from beginning.
  • If n is negative, delete from end.
Drop[ {1, 2, 3, 4, 5}, 2 ]
(* {3, 4, 5} *)

Drop[ {1, 2, 3, 4, 5}, -2 ]
(* {1, 2, 3} *)

Delete Nth Item

Drop[ expr, {n}]

delete nth item.

Drop[ {1, 2, 3, 4, 5}, {3} ]
(* {1, 2, 4, 5} *)

Delete by Index Range

Drop[ expr, {m, n}]

delete mth to nth.

Drop[ {1, 2, 3, 4, 5}, {2,4} ]
(* {1, 5} *)

Delete Parts by Position Spec

These are useful when the expression is considered as a nested list. You want to delete some nested part of the expression.

Delete[expr, pos]

delete the part at Position pos

Delete[ {a, {{b, c}, d}, e}, {1} ]
(* {{{b, c}, d}, e} *)

Delete[ {a, {{b, c}, d}, e}, {2} ]
(* {a, e} *)

Delete[ {a, {{b, c}, d}, e}, {3} ]
(* {a, {{b, c}, d}} *)

(* HHHH------------------------------ *)

Delete[ {a, {{b, c}, d}, e}, {-1} ]
(* {a, {{b, c}, d}} *)
Delete[expr, {pos1, pos2, pos3, etc}]

Delete a list of parts by Position

Delete[ {a, {{b, c}, d}, e}, {{1}} ]
(* {{{b, c}, d}, e} *)

Delete[ {a, {{b, c}, d}, e}, {{2}} ]
(* {a, e} *)

Delete[ {a, {{b, c}, d}, e}, {{1}, {2}} ]
(* {e} *)

Delete[ {a, {{b, c}, d}, e}, {{2, 1}} ]
(* {a, {d}, e} *)

Delete[ {a, {{b, c}, d}, e}, {{2, 1, 1}} ]
(* {a, {{c}, d}, e} *)
Delete[expr, n]

shortcut for

Delete[expr, {n}]

the {n} means the Position, which is same as index n

same as delete element at nth index.

Delete[ {a, {{b, c}, d}, e}, 1 ]
(* {{{b, c}, d}, e} *)

(* same as *)

Delete[ {a, {{b, c}, d}, e}, {1} ]
(* {{{b, c}, d}, e} *)

(* same as *)

Delete[ {a, {{b, c}, d}, e}, {{1}} ]
(* {{{b, c}, d}, e} *)

Delete by a Filtering Function

Delete by a Filtering Pattern

Delete Duplicates

DeleteDuplicates[expr]

Delete duplicates

DeleteDuplicates[ {3, x, 3, x, 4 } ]
(* {3, x, 4} *)

Wolfram. List Operations