# How to loop over the key/value pairs of a NamedTuple?

**URL:** <https://discourse.julialang.org/t/how-to-loop-over-the-key-value-pairs-of-a-namedtuple/5470>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 20, 2017, 4:43am UTC](https://discourse.julialang.org/t/how-to-loop-over-the-key-value-pairs-of-a-namedtuple/5470 "2017-08-20T04:43:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 20, 2017, 4:43am UTC](https://discourse.julialang.org/t/how-to-loop-over-the-key-value-pairs-of-a-namedtuple/5470/1 "2017-08-20T04:43:40Z")

</div>

I tried the following without success:

```julia
using NamedTuples

nt = @NT(a=1, b=1.0)

for (k,v) in nt
  println(k, " => ", v)
end

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 20, 2017, 7:07am UTC](https://discourse.julialang.org/t/how-to-loop-over-the-key-value-pairs-of-a-namedtuple/5470/2 "2017-08-20T07:07:20Z")

</div>

Try something like

```julia
for (k,v) in zip(keys(nt), nt)
    println(k, " => ", v)
end

```

Note that `NamedTuple`s behave like tuples in many contexts, and when you iterate over them you just get the values.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 20, 2017, 7:14am UTC](https://discourse.julialang.org/t/how-to-loop-over-the-key-value-pairs-of-a-namedtuple/5470/3 "2017-08-20T07:14:07Z")

</div>

Thank you @Tamas_Papp, I was wondering if another method existed, it seems like a common pattern for named tuples.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 20, 2017, 7:29am UTC](https://discourse.julialang.org/t/how-to-loop-over-the-key-value-pairs-of-a-namedtuple/5470/4 "2017-08-20T07:29:04Z")

</div>

However, I am not sure if it is type stable, `zip` sometimes isn’t. YMMV, and you could also use `map`.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 20, 2017, 7:40am UTC](https://discourse.julialang.org/t/how-to-loop-over-the-key-value-pairs-of-a-namedtuple/5470/5 "2017-08-20T07:40:43Z")

</div>

I opened an issue to see if someone has more ideas:

[https://github.com/blackrock/NamedTuples.jl/issues/42](https://github.com/blackrock/NamedTuples.jl/issues/42)

Is the solution with `zip` guaranteed to always match the pairs in the named tuple?

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [August 21, 2017, 7:35pm UTC](https://discourse.julialang.org/t/how-to-loop-over-the-key-value-pairs-of-a-namedtuple/5470/6 "2017-08-21T19:35:30Z")

</div>

I think @Tamas_Papp’s recommendation is the right one and I don’t think there is another way of doing this. It might be nice to add a `pairs` functions to the package that returns a `Pair` iterator.

Tamas is also right about type stability, that for loop won’t be type stable. If you use `map` instead, you should get a type stable version instead.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 21, 2017, 10:10pm UTC](https://discourse.julialang.org/t/how-to-loop-over-the-key-value-pairs-of-a-namedtuple/5470/7 "2017-08-21T22:10:11Z")

</div>

@davidanthoff I am using @Tamas_Papp’s solution, thanks.

I still have an issue with using NamedTuple objects as described in this post though:

> [@Handling solver parameters elegantly in Julia](https://discourse.julialang.org/t/handling-solver-parameters-elegantly-in-julia/5269/11):
>
> @tshort, I am back to this issue after a while. I am giving NamedTuple a try, but couldn’t make the outer constructor below work: using Parameters: @with\_kw @with\_kw struct Param mean = 0. variance = 1. end struct Solver params::Dict{Symbol,Param} # inner ctor to be called from outside Solver(params::Dict{Symbol,Param}) = new(params) end # outer ctor with named tuples function Solver(params...) dict = Dict{Symbol,Param}() for (varname, varparams) in params push!(dict, varname …

Basically, I am trying to have an outer constructor with NamedTuples that get converted into an inner constructor with dictionaries. My attempt failed.

---

<div class="post-metadata">

**Author:** ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)\
**Post date:** [April 28, 2022, 6:44am UTC](https://discourse.julialang.org/t/how-to-loop-over-the-key-value-pairs-of-a-namedtuple/5470/8 "2022-04-28T06:44:38Z")

</div>

What’s the current best way for iterating key/value pairs of a NamedTuple? `pairs` doesn’t work:

```julia
julia> for v in (a=1, b=2) println(v) end
1
2

julia> for (k,v) in pairs(a=1, b=2) println(v) end
ERROR: MethodError: no method matching pairs(; a=1, b=2)
Closest candidates are:
  pairs(::AbstractDict) at ~/julia/julia-1.7.2/share/julia/base/abstractdict.jl:140 got unsupported keyword arguments "a", "b"
  pairs(::IndexLinear, ::AbstractArray) at ~/julia/julia-1.7.2/share/julia/base/iterators.jl:226 got unsupported keyword arguments "a", "b"
  pairs(::IndexCartesian, ::AbstractArray) at ~/julia/julia-1.7.2/share/julia/base/iterators.jl:227 got unsupported keyword arguments "a", "b"
  ...
Stacktrace:
 [1] top-level scope
   @ ./REPL[14]:1

```

---

<div class="post-metadata">

**Author:** ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)\
**Post date:** [April 28, 2022, 6:47am UTC](https://discourse.julialang.org/t/how-to-loop-over-the-key-value-pairs-of-a-namedtuple/5470/9 "2022-04-28T06:47:40Z")

</div>

Actually I was doing a stupid mistake, `pairs` works:

```julia
julia> for (k,v) in pairs((a=1, b=2)) println(k => v) end
:a => 1
:b => 2

```
