# \`length.(keys(d))\` does not work?

**URL:** <https://discourse.julialang.org/t/length-keys-d-does-not-work/913>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 13, 2016, 9:03am UTC](https://discourse.julialang.org/t/length-keys-d-does-not-work/913 "2016-12-13T09:03:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [December 13, 2016, 9:03am UTC](https://discourse.julialang.org/t/length-keys-d-does-not-work/913/1 "2016-12-13T09:03:23Z")

</div>

Create a dictionary with vector keys:

```julia
d = Dict([1,2] => 1.1, [3,2] => 0.1)

```

Then I want to evalute the length of the keys, so I write:

```julia
length.(keys(d))

```

But this results in an error:

> ERROR: MethodError: no method matching size(::Base.KeyIterator{Dict{Array{Int64,1},Float64}})

What is the reason for this? Is this intentional?

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [December 13, 2016, 9:06am UTC](https://discourse.julialang.org/t/length-keys-d-does-not-work/913/2 "2016-12-13T09:06:40Z")

</div>

This works on master, but not on 0.5.0. You can file an issue on GitHub so that the fix is backported to 0.5.1 if possible.

In the meantime, you can work around this by defining this function:

```julia
Base.size(x::Base.KeyIterator) = (length(x),)

```

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [December 13, 2016, 9:18am UTC](https://discourse.julialang.org/t/length-keys-d-does-not-work/913/3 "2016-12-13T09:18:34Z")

</div>

Thanks for the reply.

[https://github.com/JuliaLang/julia/issues/19577](https://github.com/JuliaLang/julia/issues/19577)

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [December 13, 2016, 12:33pm UTC](https://discourse.julialang.org/t/length-keys-d-does-not-work/913/4 "2016-12-13T12:33:19Z")

</div>

Does

```julia
length.(collect(keys(d)))

```

do what you want?

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [December 13, 2016, 12:58pm UTC](https://discourse.julialang.org/t/length-keys-d-does-not-work/913/5 "2016-12-13T12:58:07Z")

</div>

@giordano Yes, but I expected `length.(keys(d))` to work.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [December 13, 2016, 1:03pm UTC](https://discourse.julialang.org/t/length-keys-d-does-not-work/913/6 "2016-12-13T13:03:22Z")

</div>

AFAIK, `broadcast` is supposed to work for arrays, tuples and/or scalars, not iterators (that’s what is written in the docstring of `broadcast`). `keys` returns an iterator, `collect(keys(d))` an array. But I guess you’re advocating for `broadcast` working with iterators too.

---

<div class="post-metadata">

**Author:** ![PabloZubieta](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@PabloZubieta](https://discourse.julialang.org/u/PabloZubieta)\
**Post date:** [December 13, 2016, 3:18pm UTC](https://discourse.julialang.org/t/length-keys-d-does-not-work/913/7 "2016-12-13T15:18:34Z")

</div>

On 0.5 you can get what you want with any of the following

```julia
[length(k) for k in keys(d)]
map(length, keys(d))

```

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [December 13, 2016, 7:21pm UTC](https://discourse.julialang.org/t/length-keys-d-does-not-work/913/8 "2016-12-13T19:21:40Z")

</div>

@PabloZubieta I know, but it’s longer.
