# How to select only some values of an association based on some criteria

Here is the association I have:

assc = {"abc" -> 2.456, "efg" -> 3.652, "_final" -> 6.108}


I need to remove all key-value pairs where keys start with '_'. I am new to Mathematica, so can anyone please help me out with this. I have a condition (mentioned below), I just don't know how to apply it on an association using 'select'?

The condition I have is:

(StringStartsQ[Keys[assc], "_"])

Output:= {False, False, False, False, False, False, False, False, False, \
False, False, False, False, False, True, True, True, True, True}

• Try Pick[assc, StringStartsQ[Keys[assc], Except["_"]]] – Coolwater Jun 15 '18 at 19:30
• Point of information: assc = {"abc" -> 2.456, ... } is a list of rules not an association. – Edmund Jun 15 '18 at 20:20

## Select

Select[
assc
, Not[
StringStartsQ[
Keys[#]
, "_"
]
] &
]


## DeleteCases

DeleteCases[
assc
, _?(StringStartsQ[Keys[#], "_"] &)
]


## KeySelect

Thanks to @JasonB for this solution.

KeySelect[
assc
, Not@*StringStartsQ["_"]
]

• You can shorten it a bit with KeySelect[ assc, Not@*StringStartsQ["_"]] – Jason B. Jun 15 '18 at 19:26
• @JasonB. Thanks! Didn't know about that one! Incorporated to the answer. – rhermans Jun 15 '18 at 19:29
• @rhermans: Thank you so much for your help. Unfortunately the solutions using 'Select' and 'DeleteCases' did not produce the correct result, I'm still trying on making it work. – Rajat Bhattacharjee Jun 15 '18 at 22:17
• @JasonB. Thank you! The solution worked. Much appreciated! :) – Rajat Bhattacharjee Jun 15 '18 at 22:18
• @Rajat Bhattacharjee Your edit was somewhat too extensive. That's why I voted for its rejection. However, I see that you found several caveats and their resolutions. Hence, I would like to encourage you to post your edit it as a separate answer. – Henrik Schumacher Jun 15 '18 at 22:42