Union 
Details and Options
- If the listi are considered as sets, Union gives their union.
- Union[list1,list2,…] can be input in StandardForm and InputForm as list1⋃list2⋃…. The character ⋃ can be entered as
un
or \[Union]. - The listi must have the same head, but it need not be List.
- Union[list1,…,SameTest->test] applies test to each pair of elements in the listi to determine whether they should be considered the same.
Examples
open all close allBasic Examples (3)
Scope (1)
Generalizations & Extensions (1)
Options (4)
SameTest (4)
Use equivalence classes based on absolute value:
Union[{2, -2, 1, 3, 1}, SameTest -> (Abs[#1] == Abs[#2]&)]Use equivalence classes based on Floor:
Union[{1.1, 3.4, .5, 7.6, 7.1, 1.9}, SameTest -> (Floor[#1] == Floor[#2]&)]Use Total of list elements:
Union[{{1, 2}, {3}, {4, 5, 6}, {9, 6}}, SameTest -> (Total[#1] == Total[#2]&)]Use equality of last and first elements:
Union[{{1, 2}, {2, 1}, {3, 2, 1}, {3}, {4, 5}, {5, 4, 3, 2, 1}}, SameTest -> (First[#1] == Last[#2]&)]Applications (4)
Find divisors that occur in any of 10, 12, and 20:
Union[Divisors[10], Divisors[12], Divisors[20]]Find all the triples of bits that occur in the binary decomposition of 10!:
Union[Partition[IntegerDigits[10!, 2], 3, 1]]Find the distinct elements in the iteration:
Union[NestList[Mod[5#, 11]&, 1, 100]]Find what options are used by a list of functions:
Union[Flatten[Map[Options[#][[All, 1]]&, {FindFit, FindMinimum, FindRoot}]]]Properties & Relations (3)
Split on the sorted set gives lists of the same elements:
ints = RandomInteger[9, 10]splits = Split[Sort[ints]]The union is equivalent to the first elements of these lists:
Union[ints] === splits[[All, 1]]Tally gets the count of identical elements and returns them in the original order:
ints = RandomInteger[9, 10]tally = Tally[ints]The union is the sorted list of the elements returned by Tally:
Union[ints] === Sort[tally[[All, 1]]]DeleteDuplicates is similar to Union without sorting:
list = {9, 0, 0, 3, 2, 3, 6, 2, 9, 8, 4, 9, 0, 2, 6, 5, 7, 4, 9, 8};Union[list]DeleteDuplicates[list]Sort[%]Avoiding the sort improves the speed substantially:
list = RandomInteger[10 ^ 5, 10 ^ 7];{First[Timing[DeleteDuplicates[list]]], First[Timing[Union[list]]]}Possible Issues (1)
For large sets Union may be slow with SameTest since it requires all pairwise comparisons:
ints = RandomInteger[1000, 1000];Timing[Length[u = Union[ints, SameTest -> (Floor[#1 / 2] == Floor[#2 / 2]&)]]]When equivalence class representatives can be found, it may be faster to use Union on these:
Timing[Length[fu = 2 * Union[Floor[ints / 2]]]]The results are the same except for the choice of representative:
Union[u - fu]See Also
Join Intersection Complement SymmetricDifference Tally DeleteDuplicates BinLists Split Or BitOr
Characters: \[Union]
Function Repository: MultisetUnion MultisetSum
Tech Notes
Related Guides
-
▪
- Rearranging & Restructuring Lists ▪
- Operations on Sets ▪
- Discrete & Integer Data ▪
- Math & Counting Operations on Lists ▪
- Database-Like Operations on Datasets ▪
- Finite Mathematics ▪
- Handling Arrays of Data ▪
- Discrete Mathematics ▪
- Tabular Transformation ▪
- Computation with Structured Datasets ▪
- Numerical Data
History
Introduced in 1988 (1.0) | Updated in 1996 (3.0)
Text
Wolfram Research (1988), Union, Wolfram Language function, https://reference.wolfram.com/language/ref/Union.html (updated 1996).
CMS
Wolfram Language. 1988. "Union." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 1996. https://reference.wolfram.com/language/ref/Union.html.
APA
Wolfram Language. (1988). Union. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Union.html
BibTeX
@misc{reference.wolfram_2026_union, author="Wolfram Research", title="{Union}", year="1996", howpublished="\url{https://reference.wolfram.com/language/ref/Union.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_union, organization={Wolfram Research}, title={Union}, year={1996}, url={https://reference.wolfram.com/language/ref/Union.html}, note=[Accessed: 13-June-2026]}