# Map vs list comprehension

**URL:** <https://discourse.julialang.org/t/map-vs-list-comprehension/916>\
**Category:** General Usage\
**Created:** [December 13, 2016, 12:50pm UTC](https://discourse.julialang.org/t/map-vs-list-comprehension/916 "2016-12-13T12:50:07Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [December 13, 2016, 2:39pm UTC](https://discourse.julialang.org/t/map-vs-list-comprehension/916/4 "2016-12-13T14:39:08Z")

</div>

They are not quite the same. For example, with list comprehension you can additionally filter elements:

```
[x for x in 1:10 if x % 2 == 0]

```

But `map` may be shorter and more convenient, e.g.:

```
[f(input) for input in inputs]
map(f, inputs)

```

In addition, `map` (mostly) preserves type of a collection, while list comprehencion doesn’t. Compare:

```
map(x -> x + 1, Set([1, 2, 3]))
[x + 1 for x in Set([1, 2, 3])]

```

---

_[View the full topic](https://discourse.julialang.org/t/map-vs-list-comprehension/916)._
