# What is Base.KeySet()?

**URL:** <https://discourse.julialang.org/t/what-is-base-keyset/49314>\
**Category:** New to Julia\
**Created:** [October 30, 2020, 4:23pm UTC](https://discourse.julialang.org/t/what-is-base-keyset/49314 "2020-10-30T16:23:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [October 30, 2020, 4:23pm UTC](https://discourse.julialang.org/t/what-is-base-keyset/49314/1 "2020-10-30T16:23:16Z")

</div>

When I use `keys()` the resulting data structure is a `Base.KeySet()`. Can someone elaborate on what that is? I searched the documentation, but I couldn’t find it.

```julia
character_inventory = Dict{String,Int32}("Sword" => 100, "Staff" => 50)
character_inventory_keys = keys(character_inventory)
println(typeof(character_inventory_keys))

```

**Output** :

```julia
Base.KeySet{String,Dict{String,Int32}}

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 30, 2020, 4:45pm UTC](https://discourse.julialang.org/t/what-is-base-keyset/49314/2 "2020-10-30T16:45:41Z")

</div>

The documentation for `keys` describe it a bit although it doesn’t mention `KeySet` explicitly:

```julia
help?> keys

    keys(a::AbstractDict)

Return an iterator over all keys in a dictionary. [...]

```

And to be more explicit, `KeySet` is one such iterator which yields the keys of the dictionary when iterated. It is simply a “lazy-view” of the keys (see [https://github.com/JuliaLang/julia/blob/10b0a014275ad4836fad8d2a2590d457dc2286ad/base/abstractdict.jl#L39-L41](https://github.com/JuliaLang/julia/blob/10b0a014275ad4836fad8d2a2590d457dc2286ad/base/abstractdict.jl#L39-L41)) and it is implemented like this (presumably) for efficiency since you don’t have to materialize an array or similar with all the keys.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 30, 2020, 6:01pm UTC](https://discourse.julialang.org/t/what-is-base-keyset/49314/3 "2020-10-30T18:01:05Z")

</div>

> [@anon60034542](#):
>
> When I use `keys()` with `collect()` , it resulting data structure is a `Base.KeySet()` .

I think you meant ‘use `keys()` _without_ `collect()`’, if you use `collect` you get an `Array` as expected.

---

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [October 30, 2020, 7:01pm UTC](https://discourse.julialang.org/t/what-is-base-keyset/49314/4 "2020-10-30T19:01:14Z")

</div>

Good catch, Fixed!
