# 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:** 5

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [December 23, 2016, 6:42am UTC](https://discourse.julialang.org/t/map-vs-list-comprehension/916/5 "2016-12-23T06:42:28Z")

</div>

I still having a hard time with all this (Julia v0.5).

Comprehension on an `Array{Tuple}` is fine

```julia
julia> a=[(1,2),(3,4)];
julia> [x for (x,y) in a]
2-element Array{Int64,1}:

```

but not `map`

```julia
julia> map((x,y)->x, a)
ERROR: MethodError: no method matching (::##3#4)(::Tuple{Int64,Int64})

```

even though

```julia
(x,y) = a[1]

```

is fine.

Also `filter` raises an error on

```julia
julia> filter((x,y)->x==3, a)
ERROR: MethodError: no method matching (::##5#6)(::Tuple{Int64,Int64})

```

because `a` is not an associative collection (two arguments are passed to the function in this case: this is specified in the manual). Indeed `filter` works here

```julia
julia> filter((x,y)->x==3, Dict(a))
Dict{Int64,Int64} with 1 entry:

```

Then again neither one is valid:

```julia
julia> foreach((x,y)->println(x), a)
ERROR: MethodError: no method matching (::##9#10)(::Pair{Int64,Int64})
julia> foreach((x,y)->println(x), Dict(a))
ERROR: MethodError: no method matching (::##11#12)(::Pair{Int64,Int64})

```

Instead

```julia
julia> for (x,y) in a
       println(x)
       end

```

is OK.

Then I come across this:

```julia
julia> filter((x,y)->begin println(typeof(x)); x[1]==3; end, Dict(a))
Int64
Int64
Dict{Int64,Int64} with 1 entry:

```

but it should be an error because `Int64` has no `getindex`.

I am very confused, but there must have been a good reason to have it this way and I cannot see it. How can I picture all this in a more systematic way?

(And all this because of [this post](https://discourse.julialang.org/t/pair-a-b-vs-tuple-a-b/942).)

---

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