KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Key value pairs in dictionary, and how to retrieve them given one?

  • Key value pairs in dictionary, and how to retrieve them given one?

    Posted by nvadithya on August 5, 2022 at 12:00 am

    I’m wondering on how to retrieve the key given the value of a dictionary?

    The way to get value given the key is

    array[key] – this will output the value, but what if I have the value and am looking to get the key?

    nvadithya replied 9 months, 1 week ago 3 Members · 2 Replies
  • 2 Replies
  • iodwyer

    Member
    August 5, 2022 at 12:00 am

     

    q)d:`a`b`c!1 2 3 
    q)d[`a] 
    1 
    q)d?1 
    `a 
    q)d:`a`b`c`d!1 2 3 1 
    q)d?1 
    `a

    be mindful, with this method and duplicate values, only the first key is returned

  • baichen

    Member
    August 6, 2022 at 12:00 am

    you also can use where clause, which return all keys that have duplicates:

    q)dic:`a`b`c`d!1 2 3 1
    q)where dic=1
    `a`d
    
    

Log in to reply.