---
title: "Map"
language: "en"
type: "Symbol"
summary: "Map[f, expr] or f /@ expr applies f to each element on the first level in expr. Map[f, expr, levelspec] applies f to parts of expr specified by levelspec. Map[f] represents an operator form of Map that can be applied to an expression."
keywords: 
- applying functions to lists
- applying functions to parts
- parallel form of Map
- simultaneous application
- parallel map
- mapcar
- parallelization
- parallel computation
- concurrent computation
- wrapping functions around elements of lists
- foreach
- map
- mapc
- mapcan
- mapcar
- mapcon
- mapl
- maplist
- map
- arrayfun
- cellfun
- structfun
canonical_url: "https://reference.wolfram.com/language/ref/Map.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Applying Functions to Lists"
    link: "https://reference.wolfram.com/language/guide/ApplyingFunctionsToLists.en.md"
  - 
    title: "Parts of Matrices"
    link: "https://reference.wolfram.com/language/guide/PartsOfMatrices.en.md"
  - 
    title: "List Manipulation"
    link: "https://reference.wolfram.com/language/guide/ListManipulation.en.md"
  - 
    title: "Functional Programming"
    link: "https://reference.wolfram.com/language/guide/FunctionalProgramming.en.md"
  - 
    title: "Wolfram Language Syntax"
    link: "https://reference.wolfram.com/language/guide/Syntax.en.md"
  - 
    title: "Expressions"
    link: "https://reference.wolfram.com/language/guide/Expressions.en.md"
  - 
    title: "Parts of Expressions"
    link: "https://reference.wolfram.com/language/guide/PartsOfExpressions.en.md"
  - 
    title: "Associations"
    link: "https://reference.wolfram.com/language/guide/Associations.en.md"
  - 
    title: "Function Composition & Operator Forms"
    link: "https://reference.wolfram.com/language/guide/FunctionCompositionAndOperatorForms.en.md"
  - 
    title: "Looping Constructs"
    link: "https://reference.wolfram.com/language/guide/LoopingConstructs.en.md"
  - 
    title: "Handling Arrays of Data"
    link: "https://reference.wolfram.com/language/guide/HandlingArraysOfData.en.md"
  - 
    title: "Language Overview"
    link: "https://reference.wolfram.com/language/guide/LanguageOverview.en.md"
related_functions: 
  - 
    title: "Apply"
    link: "https://reference.wolfram.com/language/ref/Apply.en.md"
  - 
    title: "Scan"
    link: "https://reference.wolfram.com/language/ref/Scan.en.md"
  - 
    title: "MapAll"
    link: "https://reference.wolfram.com/language/ref/MapAll.en.md"
  - 
    title: "MapAt"
    link: "https://reference.wolfram.com/language/ref/MapAt.en.md"
  - 
    title: "MapIndexed"
    link: "https://reference.wolfram.com/language/ref/MapIndexed.en.md"
  - 
    title: "MapApply"
    link: "https://reference.wolfram.com/language/ref/MapApply.en.md"
  - 
    title: "MapThread"
    link: "https://reference.wolfram.com/language/ref/MapThread.en.md"
  - 
    title: "SubsetMap"
    link: "https://reference.wolfram.com/language/ref/SubsetMap.en.md"
  - 
    title: "AssociationMap"
    link: "https://reference.wolfram.com/language/ref/AssociationMap.en.md"
  - 
    title: "KeyMap"
    link: "https://reference.wolfram.com/language/ref/KeyMap.en.md"
  - 
    title: "KeyValueMap"
    link: "https://reference.wolfram.com/language/ref/KeyValueMap.en.md"
  - 
    title: "ParallelMap"
    link: "https://reference.wolfram.com/language/ref/ParallelMap.en.md"
  - 
    title: "Level"
    link: "https://reference.wolfram.com/language/ref/Level.en.md"
  - 
    title: "Operate"
    link: "https://reference.wolfram.com/language/ref/Operate.en.md"
  - 
    title: "Comap"
    link: "https://reference.wolfram.com/language/ref/Comap.en.md"
  - 
    title: "Through"
    link: "https://reference.wolfram.com/language/ref/Through.en.md"
  - 
    title: "Thread"
    link: "https://reference.wolfram.com/language/ref/Thread.en.md"
  - 
    title: "ImageApply"
    link: "https://reference.wolfram.com/language/ref/ImageApply.en.md"
related_tutorials: 
  - 
    title: "Applying Functions to Parts of Expressions"
    link: "https://reference.wolfram.com/language/tutorial/FunctionalOperations.en.md#28027"
---
# Map (/@)

Map[f, expr] or f /@ expr applies f to each element on the first level in expr. 

Map[f, expr, levelspec] applies f to parts of expr specified by levelspec. 

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

## Details and Options

* ``Map`` uses standard level specifications:

|          |                           |
| -------- | ------------------------- |
| n        | levels 1 through n        |
| Infinity | levels 1 through Infinity |
| {n}      | level n only              |
| {n1, n2} | levels n1 through n2      |

* The default value for ``levelspec`` in ``Map`` is ``{1}``.

* A positive level ``n`` consists of all parts of ``expr`` specified by ``n`` indices.

* A negative level ``-n`` consists of all parts of ``expr`` with depth ``n``.

* Level ``–1`` consists of numbers, symbols, and other objects that do not have subparts.

* Level ``0`` corresponds to the whole expression.

* With the option setting ``Heads -> True``, ``Map`` includes heads of expressions and their parts.

* ``Map`` always effectively constructs a complete new expression and then evaluates it.

* If ``expr`` is an ``Association`` object, ``Map[f, expr]`` applies ``f`` to the values in the association. »

* If ``expr`` is a ``SparseArray`` object or structured array, ``Map[f, expr]`` applies ``f`` to the values or subarrays that appear in ``expr``. »

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

* ``Parallelize[Map[f, expr]]`` or ``ParallelMap[f, expr]`` computes ``Map[f, expr]`` in parallel on all subkernels. »

---

## Examples (32)

### Basic Examples (5)

Evaluate ``f`` on each element of a list:

```wl
In[1]:= Map[f, {a, b, c, d, e}]

Out[1]= {f[a], f[b], f[c], f[d], f[e]}
```

Use the short input form:

```wl
In[2]:= f /@ {a, b, c, d, e}

Out[2]= {f[a], f[b], f[c], f[d], f[e]}
```

---

Use explicit pure functions:

```wl
In[1]:= (1 + g[#])& /@ {a, b, c, d, e}

Out[1]= {1 + g[a], 1 + g[b], 1 + g[c], 1 + g[d], 1 + g[e]}

In[2]:= Function[x, x ^ 2] /@ {1, 2, 3, 4}

Out[2]= {1, 4, 9, 16}
```

---

Map at top level:

```wl
In[1]:= Map[f, {{a, b}, {c, d, e}}]

Out[1]= {f[{a, b}], f[{c, d, e}]}
```

Map at level ``2`` :

```wl
In[2]:= Map[f, {{a, b}, {c, d, e}}, {2}]

Out[2]= {{f[a], f[b]}, {f[c], f[d], f[e]}}
```

Map at levels ``1`` and ``2`` :

```wl
In[3]:= Map[f, {{a, b}, {c, d, e}}, 2]

Out[3]= {f[{f[a], f[b]}], f[{f[c], f[d], f[e]}]}
```

---

Use a map operator:

```wl
In[1]:= Map[f][{a, b, c, d}]

Out[1]= {f[a], f[b], f[c], f[d]}
```

---

Map a function over values in ``Association`` :

```wl
In[1]:= Map[h, <|a -> b, c -> d|>]

Out[1]= <|a -> h[b], c -> h[d]|>
```

### Scope (11)

#### Level Specifications (6)

Map at level ``1`` (default):

```wl
In[1]:= Map[f, {{{{{a}}}}}]

Out[1]= {f[{{{{a}}}}]}
```

Map down to level ``2`` :

```wl
In[2]:= Map[f, {{{{{a}}}}}, 2]

Out[2]= {f[{f[{{{a}}}]}]}
```

Map at level ``2`` :

```wl
In[3]:= Map[f, {{{{{a}}}}}, {2}]

Out[3]= {{f[{{{a}}}]}}
```

Map on levels ``0`` through ``2`` :

```wl
In[4]:= Map[f, {{{{{a}}}}}, {0, 2}]

Out[4]= f[{f[{f[{{{a}}}]}]}]
```

Map down to level ``3`` :

```wl
In[5]:= Map[f, {{{{{a}}}}}, 3]

Out[5]= {f[{f[{f[{{a}}]}]}]}
```

---

Map on all levels, starting at level ``1`` :

```wl
In[1]:= Map[f, {{{{{a}}}}}, Infinity]

Out[1]= {f[{f[{f[{f[{f[a]}]}]}]}]}
```

Map also at level ``0`` :

```wl
In[2]:= Map[f, {{{{{a}}}}}, {0, Infinity}]

Out[2]= f[{f[{f[{f[{f[{f[a]}]}]}]}]}]
```

---

Negative levels:

```wl
In[1]:= Map[f, {{{{{a}}}}}, -1]

Out[1]= {f[{f[{f[{f[{f[a]}]}]}]}]}

In[2]:= Map[f, {{{{{a}}}}}, -2]

Out[2]= {f[{f[{f[{f[{a}]}]}]}]}

In[3]:= Map[f, {{{{{a}}}}}, -3]

Out[3]= {f[{f[{f[{{a}}]}]}]}
```

---

Positive and negative levels can be mixed:

```wl
In[1]:= Map[f, {{{{{a}}}}}, {2, -3}]

Out[1]= {{f[{f[{{a}}]}]}}
```

---

Different heads at each level:

```wl
In[1]:= Map[f, h0[h1[h2[h3[h4[a]]]]], {2, -3}]

Out[1]= h0[h1[f[h2[f[h3[h4[a]]]]]]]
```

---

Include heads in the levels specified:

```wl
In[1]:= Map[f, {{{{a}}}}, 2, Heads -> True]

Out[1]= f[List][f[f[List][f[{{a}}]]]]
```

#### Types of Expressions (5)

``Map`` can be used on expressions with any head:

```wl
In[1]:= Map[f, a + b + c + d]

Out[1]= f[a] + f[b] + f[c] + f[d]

In[2]:= Map[f, x ^ 2 + y ^ 2, 2]

Out[2]= f[f[x]^f[2]] + f[f[y]^f[2]]
```

---

``Map`` can be used on sparse arrays:

```wl
In[1]:= SparseArray[{1 -> 1, 2 -> 2, 10 -> 10}]

Out[1]= SparseArray[Automatic, {10}, 0, {1, {{0, 3}, {{1}, {2}, {10}}}, {1, 2, 10}}]

In[2]:= Map[f, %]

Out[2]= SparseArray[Automatic, {10}, f[0], {1, {{0, 3}, {{1}, {2}, {10}}}, {f[1], f[2], f[10]}}]

In[3]:= Normal[%]

Out[3]= {f[1], f[2], f[0], f[0], f[0], f[0], f[0], f[0], f[0], f[10]}
```

---

Use ``Map`` with structured arrays, such as ``SymmetrizedArray`` :

```wl
In[1]:= SymmetrizedArray[{{1, 2} -> 1, {1, 3} -> 2, {2, 3} -> 3}, {3, 3}, Antisymmetric[All]]

Out[1]=
SymmetrizedArray[StructuredArray`StructuredData[{3, 3}, {{{1, 2} -> 1, {1, 3} -> 2, {2, 3} -> 3}, 
   Antisymmetric[{1, 2}]}]]

In[2]:= f /@ %

Out[2]= {f[SymmetrizedArray[StructuredArray`StructuredData[{3}, {{{2} -> 1, {3} -> 2}, {}}]]], f[SymmetrizedArray[StructuredArray`StructuredData[{3}, {{{1} -> -1, {3} -> 3}, {}}]]], f[SymmetrizedArray[StructuredArray`StructuredData[{3}, {{{1} -> -2, {2} -> -3}, {}}]]]}
```

Use ``Map`` to apply a function to the elements of a structured array of type ``QuantityArray`` :

```wl
In[3]:= QuantityArray[{{1, 2}, {3, 4}, {5, 6}}, "Meters"]

Out[3]=
QuantityArray[StructuredArray`StructuredData[{3, 2}, {{{1, 2}, {3, 4}, {5, 6}}, "Meters", 
   {{1}, {2}}}]]

In[4]:= Map[g, %, {2}]

Out[4]= {{g[Quantity[1, "Meters"]], g[Quantity[2, "Meters"]]}, {g[Quantity[3, "Meters"]], g[Quantity[4, "Meters"]]}, {g[Quantity[5, "Meters"]], g[Quantity[6, "Meters"]]}}
```

---

Map at the second level of a nested ``Association`` :

```wl
In[1]:= Map[h, <|a -> <|b -> c|>, d -> <|e -> f|>|>, {2}]

Out[1]= <|a -> <|b -> h[c]|>, d -> <|e -> h[f]|>|>
```

---

Map at several levels in an ``Association`` :

```wl
In[1]:= Map[h, <|a -> <|b -> c|>, d -> {<|e -> f|>}|>, {1, 3}]

Out[1]= <|a -> h[<|b -> h[c]|>], d -> h[{h[<|e -> h[f]|>]}]|>
```

### Options (1)

#### Heads (1)

By default, the function is not mapped onto the heads:

```wl
In[1]:= Map[f, {a, b, c}]

Out[1]= {f[a], f[b], f[c]}

In[2]:= Map[f, {a, b, c}, Heads -> True]

Out[2]= f[List][f[a], f[b], f[c]]
```

### Applications (4)

Reverse all sublists:

```wl
In[1]:= Reverse /@ {{a, b}, {c, d}, {e, f}}

Out[1]= {{b, a}, {d, c}, {f, e}}
```

---

Add the same vector to every vector in a list:

```wl
In[1]:= Map[(# + {x, y})&, {{1, 1}, {2, 2}, {3, 3}, {4, 4}}]

Out[1]= {{1 + x, 1 + y}, {2 + x, 2 + y}, {3 + x, 3 + y}, {4 + x, 4 + y}}
```

---

Frame integers that are prime:

```wl
In[1]:= If[PrimeQ[#], Framed[#], #]& /@ Range[20]

Out[1]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
```

---

Supply additional constant arguments by using a pure function:

```wl
In[1]:= Map[f[#, 3]&, {a, b, c}]

Out[1]= {f[a, 3], f[b, 3], f[c, 3]}
```

### Properties & Relations (9)

A function of several arguments can be mapped with ``MapThread`` :

```wl
In[1]:= MapThread[f, {{1, 2, 3}, {a, b, c}}]

Out[1]= {f[1, a], f[2, b], f[3, c]}
```

---

``MapIndexed`` passes the index of an element to the mapped function:

```wl
In[1]:= MapIndexed[f, {a, b, c}]

Out[1]= {f[a, {1}], f[b, {2}], f[c, {3}]}
```

---

``MapAll`` is equivalent to a specific level specification in ``Map`` :

```wl
In[1]:= Map[f, {{a, b}, {c}, {{d}}}, {0, ∞}]

Out[1]= f[{f[{f[a], f[b]}], f[{f[c]}], f[{f[{f[d]}]}]}]

In[2]:= MapAll[f, {{a, b}, {c}, {{d}}}]

Out[2]= f[{f[{f[a], f[b]}], f[{f[c]}], f[{f[{f[d]}]}]}]
```

---

``Scan`` does the same as ``Map``, but without returning a result:

```wl
In[1]:= Map[Print, {a, b}]

During evaluation of In[1]:= a

During evaluation of In[1]:= b

Out[1]= {Null, Null}

In[2]:= Scan[Print, {a, b}]

During evaluation of In[2]:= a

During evaluation of In[2]:= b
```

---

Functions with attribute ``Listable`` are mapped automatically:

```wl
In[1]:= Attributes[Sqrt]

Out[1]= {Listable, NumericFunction, Protected}

In[2]:= Sqrt[{1, 2, 3, 4}]

Out[2]= {1, Sqrt[2], Sqrt[3], 2}

In[3]:= Map[Sqrt, {1, 2, 3, 4}]

Out[3]= {1, Sqrt[2], Sqrt[3], 2}
```

---

``ParallelMap`` computes ``Map`` in parallel:

```wl
In[1]:= ParallelMap[PrimeQ[2 ^ # - 1]&, Range[100, 110]]

Out[1]= {False, False, False, False, False, False, False, True, False, False, False}
```

``Map`` can be parallelized automatically, effectively using ``ParallelMap`` :

```wl
In[2]:= Parallelize[Map[PrimeQ[2 ^ # - 1]&, Range[100, 110]]]

Out[2]= {False, False, False, False, False, False, False, True, False, False, False}
```

---

``Map`` wraps an expression around parts of another expression:

```wl
In[1]:= Map[f, {x, y, z}]

Out[1]= {f[x], f[y], f[z]}
```

``Comap`` wraps parts of an expression around another expression:

```wl
In[2]:= Comap[{f, g, h}, x]

Out[2]= {f[x], g[x], h[x]}
```

---

``Map`` maps a function over the values in an association:

```wl
In[1]:= Map[f, <|a -> 1, b -> 2, c -> 3|>]

Out[1]= <|a -> f[1], b -> f[2], c -> f[3]|>
```

``KeyMap`` maps a function over the keys in an association:

```wl
In[2]:= KeyMap[f, <|a -> 1, b -> 2, c -> 3|>]

Out[2]= <|f[a] -> 1, f[b] -> 2, f[c] -> 3|>
```

``KeyValueMap`` maps a function over the keys and values in an association (and returns a list):

```wl
In[3]:= KeyValueMap[f, <|a -> 1, b -> 2, c -> 3|>]

Out[3]= {f[a, 1], f[b, 2], f[c, 3]}
```

``AssociationMap`` maps a function over the rules in an association:

```wl
In[4]:= AssociationMap[Reverse, <|a -> 1, b -> 2, c -> 3|>]

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

---

``Map[f, assoc]`` is equivalent to ``AssociationThread[Keys[assoc] -> Map[f, Values[assoc]]]`` :

```wl
In[1]:= assoc = <|1 -> a, 2 -> b, 3 -> c|>;

In[2]:= Map[f, assoc] === AssociationThread[Keys[assoc] -> Map[f, Values[assoc]]]

Out[2]= True
```

### Possible Issues (1)

``Map`` by default starts at level ``1``, so does not apply the function to the whole expression:

```wl
In[1]:= Map[f, h1[h2[h3[x]]], -1]

Out[1]= h1[f[h2[f[h3[f[x]]]]]]

In[2]:= Map[f, h1[h2[h3[x]]], {0, -1}]

Out[2]= f[h1[f[h2[f[h3[f[x]]]]]]]
```

### Neat Examples (1)

Show nesting structure of an expression:

```wl
In[1]:= Integrate[1 / (x ^ 3 - 1), x]

Out[1]= -(ArcTan[(1 + 2 x/Sqrt[3])]/Sqrt[3]) + (1/3) Log[-1 + x] - (1/6) Log[1 + x + x^2]

In[2]:= Map[Framed, %, Infinity]

Out[2]= -1 ArcTan[3^-(1/2) 1 + 2 x] 3^-(1/2) + (1/3) Log[-1 + x] + -(1/6) Log[1 + x + x^2]
```

## See Also

* [`Apply`](https://reference.wolfram.com/language/ref/Apply.en.md)
* [`Scan`](https://reference.wolfram.com/language/ref/Scan.en.md)
* [`MapAll`](https://reference.wolfram.com/language/ref/MapAll.en.md)
* [`MapAt`](https://reference.wolfram.com/language/ref/MapAt.en.md)
* [`MapIndexed`](https://reference.wolfram.com/language/ref/MapIndexed.en.md)
* [`MapApply`](https://reference.wolfram.com/language/ref/MapApply.en.md)
* [`MapThread`](https://reference.wolfram.com/language/ref/MapThread.en.md)
* [`SubsetMap`](https://reference.wolfram.com/language/ref/SubsetMap.en.md)
* [`AssociationMap`](https://reference.wolfram.com/language/ref/AssociationMap.en.md)
* [`KeyMap`](https://reference.wolfram.com/language/ref/KeyMap.en.md)
* [`KeyValueMap`](https://reference.wolfram.com/language/ref/KeyValueMap.en.md)
* [`ParallelMap`](https://reference.wolfram.com/language/ref/ParallelMap.en.md)
* [`Level`](https://reference.wolfram.com/language/ref/Level.en.md)
* [`Operate`](https://reference.wolfram.com/language/ref/Operate.en.md)
* [`Comap`](https://reference.wolfram.com/language/ref/Comap.en.md)
* [`Through`](https://reference.wolfram.com/language/ref/Through.en.md)
* [`Thread`](https://reference.wolfram.com/language/ref/Thread.en.md)
* [`ImageApply`](https://reference.wolfram.com/language/ref/ImageApply.en.md)

## Tech Notes

* [Applying Functions to Parts of Expressions](https://reference.wolfram.com/language/tutorial/FunctionalOperations.en.md#28027)

## Related Guides

* [Applying Functions to Lists](https://reference.wolfram.com/language/guide/ApplyingFunctionsToLists.en.md)
* [Parts of Matrices](https://reference.wolfram.com/language/guide/PartsOfMatrices.en.md)
* [List Manipulation](https://reference.wolfram.com/language/guide/ListManipulation.en.md)
* [Functional Programming](https://reference.wolfram.com/language/guide/FunctionalProgramming.en.md)
* [Wolfram Language Syntax](https://reference.wolfram.com/language/guide/Syntax.en.md)
* [`Expressions`](https://reference.wolfram.com/language/guide/Expressions.en.md)
* [Parts of Expressions](https://reference.wolfram.com/language/guide/PartsOfExpressions.en.md)
* [`Associations`](https://reference.wolfram.com/language/guide/Associations.en.md)
* [Function Composition & Operator Forms](https://reference.wolfram.com/language/guide/FunctionCompositionAndOperatorForms.en.md)
* [Looping Constructs](https://reference.wolfram.com/language/guide/LoopingConstructs.en.md)
* [Handling Arrays of Data](https://reference.wolfram.com/language/guide/HandlingArraysOfData.en.md)
* [Language Overview](https://reference.wolfram.com/language/guide/LanguageOverview.en.md)

## Related Links

* [Fast Introduction for Programmers: Applying Functions](http://www.wolfram.com/language/fast-introduction-for-programmers/applying-functions/)
* [An Elementary Introduction to the Wolfram Language: Ways to Apply Functions](https://www.wolfram.com/language/elementary-introduction/25-ways-to-apply-functions.html)
* [NKS\|Online](http://www.wolframscience.com/nks/search/?q=Map)
* [A New Kind of Science](http://www.wolframscience.com/nks/)

## History

* Introduced in 1988 (1.0) \| Updated in 2003 (5.0) ▪ [2014 (10.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn100.en.md)