# 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:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [July 4, 2017, 9:20pm UTC](https://discourse.julialang.org/t/map-vs-list-comprehension/916/21 "2017-07-04T21:20:19Z")

</div>

Sorry, I was not clear. My program was behaving badly. The new “map” is fine. I really meant “bug”. It’s a long story why, but I was not getting useful error messages, only mysterious errors. Took a while to figure out.

---

<div class="post-metadata">

**Author:** ![isentropic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isentropic/32/11894_2.png) [@isentropic](https://discourse.julialang.org/u/isentropic)\
**Post date:** [December 11, 2019, 5:42am UTC](https://discourse.julialang.org/t/map-vs-list-comprehension/916/22 "2019-12-11T05:42:10Z")

</div>

Let’s not forget that in most cases simple broadcasting fits the needs:

```julia
myFunction.(collection_of_stuff) = map(myFunction, collection_of_stuff) = [myFunction(i) for i in collection_of_stuff]

```

---

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [December 11, 2019, 10:00am UTC](https://discourse.julialang.org/t/map-vs-list-comprehension/916/23 "2019-12-11T10:00:43Z")

</div>

Is there any advantage of one of these methods over the other?

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [December 11, 2019, 12:22pm UTC](https://discourse.julialang.org/t/map-vs-list-comprehension/916/24 "2019-12-11T12:22:11Z")

</div>

I do not think so. For instance,

```julia
julia> a=[(x, x) for x in rand(100_000)];

julia> @btime map(x->first(x), a);
  91.210 μs (3 allocations: 781.34 KiB)

julia> @btime map(first, a);
  90.960 μs (3 allocations: 781.34 KiB)

julia> @btime first.(a);
  90.774 μs (5 allocations: 781.39 KiB)

```

The results are equivalent, both in output and time/memory.  
The first two are exactly the same, when you can called directly the function, it is easier to the second option. The second one is more readable for people not used to Julia, but when you are getting used to the “dot notation”, you have tendency to prefer the last one.

[Previous page](https://discourse.julialang.org/t/map-vs-list-comprehension/916.md?page=1)
