---
title: "Merge"
language: "en"
type: "Symbol"
summary: "Merge[{assoc1, assoc2, ...}, f] merges the associations associ, using the function f to combine values with the same key. Merge[{key1 -> val1, key2 -> val2, ...}, f] gives an association in which values corresponding to identical keys are combined using f. Merge[f] represents an operator form of Merge that can be applied to an expression."
keywords: 
- reduce
- aggregate
- group by
- combine
- accumulate
- map reduce
- mapreduce
canonical_url: "https://reference.wolfram.com/language/ref/Merge.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Associations"
    link: "https://reference.wolfram.com/language/guide/Associations.en.md"
  - 
    title: "Computation with Structured Datasets"
    link: "https://reference.wolfram.com/language/guide/ComputationWithStructuredDatasets.en.md"
  - 
    title: "Database-Like Operations on Datasets"
    link: "https://reference.wolfram.com/language/guide/DatabaseLikeOperationsOnDatasets.en.md"
  - 
    title: "Language Overview"
    link: "https://reference.wolfram.com/language/guide/LanguageOverview.en.md"
related_functions: 
  - 
    title: "GroupBy"
    link: "https://reference.wolfram.com/language/ref/GroupBy.en.md"
  - 
    title: "KeyIntersection"
    link: "https://reference.wolfram.com/language/ref/KeyIntersection.en.md"
  - 
    title: "KeyUnion"
    link: "https://reference.wolfram.com/language/ref/KeyUnion.en.md"
  - 
    title: "Counts"
    link: "https://reference.wolfram.com/language/ref/Counts.en.md"
  - 
    title: "CountsBy"
    link: "https://reference.wolfram.com/language/ref/CountsBy.en.md"
  - 
    title: "Transpose"
    link: "https://reference.wolfram.com/language/ref/Transpose.en.md"
  - 
    title: "GatherBy"
    link: "https://reference.wolfram.com/language/ref/GatherBy.en.md"
  - 
    title: "JoinAcross"
    link: "https://reference.wolfram.com/language/ref/JoinAcross.en.md"
---
# Merge

Merge[{assoc1, assoc2, …}, f] merges the associations associ, using the function f to combine values with the same key. 

Merge[{key1 -> val1, key2 -> val2, …}, f] gives an association in which values corresponding to identical keys are combined using f. 

Merge[f] represents an operator form of Merge that can be applied to an expression.

## Details

* ``Merge`` can operate on any combination of associations and lists of rules.

* In ``Merge[expr, f]``, the function ``f`` is applied to lists of values that share the same key.

* ``Merge[f][expr]`` is equivalent to ``Merge[expr, f]``.

## Examples (6)

### Basic Examples (4)

Merge two associations, using ``Total`` to combine elements with the same key:

```wl
In[1]:= Merge[{<|a -> 1, b -> 2|>, <|a -> 5, b -> 10|>}, Total]

Out[1]= <|a -> 6, b -> 12|>
```

---

Combine values corresponding to identical keys into lists:

```wl
In[1]:= Merge[{<|a -> 1, b -> 2|>, <|b -> 4, c -> 5|>}, Identity]

Out[1]= <|a -> {1}, b -> {2, 4}, c -> {5}|>
```

---

Combine rules with the same left-hand side:

```wl
In[1]:= Merge[{a -> x, a -> y, b -> z}, f]

Out[1]= <|a -> f[{x, y}], b -> f[{z}]|>
```

---

Create a ``Merge`` operator:

```wl
In[1]:= merge = Merge[Total];

In[2]:= merge[{<|a -> 1, b -> 2|>, <|a -> 3, b -> 1|>}]

Out[2]= <|a -> 4, b -> 3|>
```

### Scope (1)

Combine mixed list entries:

```wl
In[1]:= Merge[{<|a -> x|>, a -> y, {b -> z}}, Identity]

Out[1]= <|a -> {x, y}, b -> {z}|>
```

### Properties & Relations (1)

Using a combiner function is equivalent to using ``Map`` :

```wl
In[1]:= Merge[{<|a -> 1|>, <|a -> 2, d -> 1|>}, Total]

Out[1]= <|a -> 3, d -> 1|>

In[2]:= Map[Total, Merge[{<|a -> 1|>, <|a -> 2, d -> 1|>}, Identity]]

Out[2]= <|a -> 3, d -> 1|>
```

## See Also

* [`GroupBy`](https://reference.wolfram.com/language/ref/GroupBy.en.md)
* [`KeyIntersection`](https://reference.wolfram.com/language/ref/KeyIntersection.en.md)
* [`KeyUnion`](https://reference.wolfram.com/language/ref/KeyUnion.en.md)
* [`Counts`](https://reference.wolfram.com/language/ref/Counts.en.md)
* [`CountsBy`](https://reference.wolfram.com/language/ref/CountsBy.en.md)
* [`Transpose`](https://reference.wolfram.com/language/ref/Transpose.en.md)
* [`GatherBy`](https://reference.wolfram.com/language/ref/GatherBy.en.md)
* [`JoinAcross`](https://reference.wolfram.com/language/ref/JoinAcross.en.md)

## Related Guides

* [`Associations`](https://reference.wolfram.com/language/guide/Associations.en.md)
* [Computation with Structured Datasets](https://reference.wolfram.com/language/guide/ComputationWithStructuredDatasets.en.md)
* [Database-Like Operations on Datasets](https://reference.wolfram.com/language/guide/DatabaseLikeOperationsOnDatasets.en.md)
* [Language Overview](https://reference.wolfram.com/language/guide/LanguageOverview.en.md)

## Related Links

* [An Elementary Introduction to the Wolfram Language: Associations](https://www.wolfram.com/language/elementary-introduction/34-associations.html)

## History

* [Introduced in 2014 (10.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn100.en.md)