Stream: helpdesk (published)

Topic: ✔ How to get values from `Pairs`?


view this post on Zulip Júlio Hoffimann (Jul 24 2025 at 13:10):

That should be trivial, but after a few minutes trying, I still can't figure it out:

p = pairs((a=1,b=2))

keys(p) # works
values(p) # fails: returns named tuple

The documentation of Pairs is non-existent in the manual.

view this post on Zulip Júlio Hoffimann (Jul 24 2025 at 13:14):

One possible solution:

[pi.second for pi in p]

view this post on Zulip Andy Dienes (Jul 24 2025 at 13:14):

Iterators.map(last, p) will do it

view this post on Zulip Andy Dienes (Jul 24 2025 at 13:15):

keys and values API is a total mess

view this post on Zulip Andy Dienes (Jul 24 2025 at 13:15):

I looked at it once and then got really mad and had to stop looking

view this post on Zulip Júlio Hoffimann (Jul 24 2025 at 13:15):

It is :grapes:

view this post on Zulip Andy Dienes (Jul 24 2025 at 13:17):

actually this was just about my very first ever submitted GH issue https://github.com/JuliaLang/julia/issues/49932

view this post on Zulip Mason Protter (Jul 24 2025 at 13:34):

Why is it returning a NamedTuple a failure?

view this post on Zulip Mason Protter (Jul 24 2025 at 13:35):

For better or worse, NamedTuple does act like a value collection, not a key-value collection:

julia> v = values(p)
(a = 1, b = 2)

julia> for x in v
           println(x)
       end
1
2

view this post on Zulip Júlio Hoffimann (Jul 24 2025 at 13:35):

I think it comes from the context. I am bumping into this when I am processing kwargs:

f(; kwargs...) = kwargs

Inside the function f I would like to extract the names of the keywords and their values.

view this post on Zulip Júlio Hoffimann (Jul 24 2025 at 13:36):

So I would expect kwargs to behave like a named tuple.

view this post on Zulip Mason Protter (Jul 24 2025 at 13:36):

the NamedTuple is a collection of values though

view this post on Zulip Júlio Hoffimann (Jul 24 2025 at 13:36):

I know. It is a bit weird though.

view this post on Zulip Mason Protter (Jul 24 2025 at 13:38):

Fair enough

view this post on Zulip Notification Bot (Jul 24 2025 at 13:48):

Júlio Hoffimann has marked this topic as resolved.

view this post on Zulip aplavin (Jul 24 2025 at 16:08):

values(values(kwargs)) if you want a Tuple?

view this post on Zulip Júlio Hoffimann (Jul 24 2025 at 16:09):

Also an option. Thanks.

view this post on Zulip Júlio Hoffimann (Jul 24 2025 at 16:10):

I wish all these inconsistencies were gone in the future after careful review of iteration on these objects


Last updated: Aug 14 2025 at 04:51 UTC