# Check if NDSparse key exists

**URL:** <https://discourse.julialang.org/t/check-if-ndsparse-key-exists/23488>\
**Category:** General Usage\
**Tags:** juliadb\
**Created:** [April 24, 2019, 9:58pm UTC](https://discourse.julialang.org/t/check-if-ndsparse-key-exists/23488 "2019-04-24T21:58:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jekyllstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jekyllstein/32/4157_2.png) [@jekyllstein](https://discourse.julialang.org/u/jekyllstein)\
**Post date:** [April 24, 2019, 9:58pm UTC](https://discourse.julialang.org/t/check-if-ndsparse-key-exists/23488/1 "2019-04-24T21:58:19Z")

</div>

If I have an NDSparse table, can I check if a key exists similar to dictionaries? With a dictionary (d) I can use `haskey(d)` but with a table (tab) `haskey(tab)` results in an error. I know I can do a try catch statement with `tab[key1]` but is there a more elegant and efficient way to check ahead of time if a key is available?

---

<div class="post-metadata">

**Author:** ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)\
**Post date:** [April 24, 2019, 11:17pm UTC](https://discourse.julialang.org/t/check-if-ndsparse-key-exists/23488/2 "2019-04-24T23:17:17Z")

</div>

Are you using the latest version of IndexedTables? That should work since [https://github.com/JuliaComputing/IndexedTables.jl/pull/215](https://github.com/JuliaComputing/IndexedTables.jl/pull/215) .

```julia
julia> x = ndsparse(["a","b"], [3,4])
1-d NDSparse with 2 values (Int64):
1 │
────┼──
"a" │ 3
"b" │ 4

julia> keys(x)
2-element StructArrays.StructArray{Tuple{String},1,NamedTuple{(:x1,),Tuple{Array{String,1}}}}:
 ("a",)
 ("b",)
julia> haskey(x, ("a",))
true

```

The tuple is important since the keys can be multi valued.

---

<div class="post-metadata">

**Author:** ![jekyllstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jekyllstein/32/4157_2.png) [@jekyllstein](https://discourse.julialang.org/u/jekyllstein)\
**Post date:** [April 24, 2019, 11:34pm UTC](https://discourse.julialang.org/t/check-if-ndsparse-key-exists/23488/3 "2019-04-24T23:34:58Z")

</div>

I’m using IndexedTables v0.12.0 but I didn’t install it myself. It is just the version that came with the latest JuliaDB. I don’t wanna mess up that installation. Is the branch you’re talking about what would be used by default?

this is what I get when I try that:

```nohighlight
julia> x = ndsparse(["a", "b"], [3, 4])
1-d NDSparse with 2 values (Int64):
1 │
────┼──
"a" │ 3
"b" │ 4

julia> keys(x)
2-element StructArrays.StructArray{Tuple{String},1,NamedTuple{(:x1,),Tuple{Array{String,1}}}}:
 ("a",)
 ("b",)

julia> haskey(x, "a")
ERROR: MethodError: no method matching haskey(::NDSparse{Int64,Tuple{String},StructArrays.StructArray{Tuple{String},1,NamedTuple{(:x1,),Tuple{Array{String,1}}}},Array{Int64,1}}, ::String)
Closest candidates are:
  haskey(::REPL.Terminals.TTYTerminal, ::Any) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.1\REPL\src\Terminals.jl:174
  haskey(::LibGit2.CachedCredentials, ::Any) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.1\LibGit2\src\types.jl:1274
  haskey(::Pkg.TOML.Table, ::AbstractString) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.1\Pkg\ext\TOML\src\parser.jl:32
  ...
Stacktrace:
 [1] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)\
**Post date:** [April 24, 2019, 11:46pm UTC](https://discourse.julialang.org/t/check-if-ndsparse-key-exists/23488/4 "2019-04-24T23:46:13Z")

</div>

It needs to be `("a",)` so it’s a tuple.

---

<div class="post-metadata">

**Author:** ![jekyllstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jekyllstein/32/4157_2.png) [@jekyllstein](https://discourse.julialang.org/u/jekyllstein)\
**Post date:** [April 24, 2019, 11:55pm UTC](https://discourse.julialang.org/t/check-if-ndsparse-key-exists/23488/5 "2019-04-24T23:55:14Z")

</div>

Ah thank you.
