Discard[data,critprop]
returns the property prop of the remaining elements.
Discard
Discard[data,critprop]
returns the property prop of the remaining elements.
Details
- Discard keeps the elements for which the given criterion crit is False.
- The data can have the following forms and interpretations:
-
{e1,e2,…} list of values » f[e1,e2,…] any head f » Association[…] association of values » QuantityArray[…] quantity array or other structured array Tabular[…] type-consistent tabular data » TabularColumn[…] type-consistent column data » Dataset[…] general hierarchical data TimeSeries[…] collection of sampled time-value pairs EventSeries[…] series of temporal events - The property prop can have the following forms and interpretations:
-
"Element" the remaining values » "Index" indices of the remaining values » "BitVectorMask" Boolean mask returning True for selected values and False otherwise » {prop1,prop2,…} a list of multiple forms » All an association giving element, index and bit vector mask » - Discard[crit][data] is equivalent to Discard[data,crit].
Examples
open all close allBasic Examples (6)
Discard elements that are even:
Discard[{1, 2, 4, 7, 6, 2}, EvenQ]Return the indices of the remaining elements:
Discard[{1, 2, 4, 7, 6, 2}, # > 2& -> "Index"]Discard only the first expression selected:
Discard[{1, 2, 4, 7, 6, 2}, OddQ, 1]Use the operator form of Discard:
Discard[EvenQ][{1, 2, 4, 7, 6, 2}]Discard operates on values in an Association:
Discard[<|a -> 1, b -> 2, c -> 3, d -> 4|>, # > 2&]Discard rows in a Tabular object:
Tabular[{{"cat", 2}, {"fox", 3}, {"dog", 5}}, {"a", "b"}]Discard[%, OddQ[#b]&]Scope (15)
Basic Uses (5)
Discard[{{1, y}, {2, x}, {3, x}, {4, z}, {5, x}, {6, y}}, MemberQ[#, x]&]Remove up to two pairs containing x:
Discard[{{1, y}, {2, x}, {3, x}, {4, z}, {5, x}, {6, y}}, MemberQ[#, x]&, 2]Use a pure function to test each element:
Discard[{1, 2, 4, 7, 6, 2}, # > 2&]Use an operator form as selection criterion:
Discard[Range[10], GreaterThan[3]]Use Discard in operator form:
Range[10]//Select[GreaterThan[3]]Input Data (5)
Discard works with any head, not just List:
Discard[f[1, a, 2, b, 3], IntegerQ]Discard works on values in Association:
Discard[<|"a" -> {.1, .3, .2}, "b" -> {"dog", "cat", "fox"}|>, AllTrue[#, StringQ]&]Discard works with vector SparseArray objects:
s = SparseArray[Table[2 ^ i -> i, {i, 0, 4}]]Discard[s, OddQ]The result may be a list if it is not sparse:
Discard[s, EvenQ]Discard works with TabularColumn objects:
TabularColumn[RandomDate[10 ^ 5]]Remove all dates with even days:
Discard[EvenQ[DateValue[#, "Day"]]&][%]The distribution of the dates is uniform:
DateHistogram[Normal[%]]Use Discard on a Tabular object with named columns:
tab = ToTabular[{"a" -> {1, 2, 3}, "b" -> {.1, .3, .2}, "c" -> {"dog", "cat", "fox"}, "d" -> {DateObject[{2024, 8, 1}], DateObject[{2024, 8, 2}], DateObject[{2024, 8, 3}]}}, "Columns"]Discard rows with last column date being a business day:
Discard[tab, Function[BusinessDayQ[#d]]]Normal[%]Property Forms (5)
Return the remaining elements:
Discard[Range[10], OddQ -> "Element"]Return the indices of the remaining elements:
Discard[Range[10], OddQ -> "Index"]Return the Boolean mask of the data:
Discard[Range[10], OddQ -> "BitVectorMask"]Normal[%]Discard[Range[10], OddQ -> {"Element", "Index"}]Return the association with all the properties:
Discard[Range[10], OddQ -> All]Applications (8)
Discard numbers up to 100 that are divisible by 4:
Discard[Range[100], Mod[#, 4] == 0&]Discard the first 3 of 4-tuples that read the same in reverse:
Discard[Tuples[{a, b}, 4], # == Reverse[#]&, 3]Discard 3×3 matrices of 0s and 1s that have determinant less than 2:
Discard[Tuples[{0, 1}, {3, 3}], Det[#] < 2&]Discard eigenvalues that lie within the unit circle:
Discard[Eigenvalues[RandomReal[1, {5, 5}]], Abs[#] < 1&]Find built-in Wolfram Language objects whose names are at least 36 characters long:
Discard[Names["*"], StringLength[#] < 36&]Remove numeric quantities from a product:
Discard[7 π^2 x^2 y^2, NumericQ]Find an approximation to
by finding the proportion of points that lie within a disk:
app[n_] := 4N[Length[Discard[RandomReal[{-1, 1}, {n, 2}], Dot[#, #] > 1&]] / n]TableForm[Table[n = 10 ^ k;p = app[n];{n, p, π - p}, {k, 1, 6}], TableHeadings -> {{}, {"n", "approximation", "error"}}]Weather data from JFK airport:
data = Tabular[{...}]Use TabularStructure to investigate the number of missing data:
TabularStructure[data]Remove the rows with at least two missing values:
Discard[data, Count[#, _Missing] >= 2&]TabularStructure[%]Properties & Relations (3)
Discard is complementary to Select:
data = RandomInteger[100, 10]Discard[data, PrimeQ]Select[data, PrimeQ]Union[%%, %] == Union@dataDiscard is similar to Cases except that it uses a function instead of a pattern:
list = RandomInteger[9, {10, 2}]Discard the lists that have a sum of elements greater than 10:
f = Total[#] > 10&;Discard[list, f]Use Cases to get the same result:
Cases[list, x_ /; !f[x]]Discard elements that are even:
Discard[Range[10], EvenQ]Use GatherBy to separate odd and even elements:
GatherBy[Range[10], EvenQ]Use GroupBy to construct an association explicitly containing as keys the results of the criterion:
GroupBy[Range[10], EvenQ]Possible Issues (2)
Discard drops elements for which applying the criterion explicitly yields True:
Discard[{1, 2, 4, 7, x}, # > 2&]Discard[{1, 2, 4, 7, x}, # <= 2&]The symbolic object x is included in both cases, since neither inequality gives True:
{x > 2, x <= 2}The head of the input expression is only preserved for the default "Element" property:
Discard[f@@{1, 2, 4, 7, 8}, # > 2& -> All]See Also
Related Guides
Text
Wolfram Research (2025), Discard, Wolfram Language function, https://reference.wolfram.com/language/ref/Discard.html (updated 2026).
CMS
Wolfram Language. 2025. "Discard." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2026. https://reference.wolfram.com/language/ref/Discard.html.
APA
Wolfram Language. (2025). Discard. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Discard.html
BibTeX
@misc{reference.wolfram_2026_discard, author="Wolfram Research", title="{Discard}", year="2026", howpublished="\url{https://reference.wolfram.com/language/ref/Discard.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_discard, organization={Wolfram Research}, title={Discard}, year={2026}, url={https://reference.wolfram.com/language/ref/Discard.html}, note=[Accessed: 13-June-2026]}