7
$\begingroup$

I'm trying to remove the key and respective value from an association where the key starts with an underscore.

So some association:

asso = <|"key1"->{}, "key2"->"string", "_key3"->3, 
    "_key4"-><|"key5">"otherString","_key6"->"otherOtherString"|>
|>

should become just:

<|"key1"->{},"key2"->"string"|>

Note everything in the value for _key4 is removed.

$\endgroup$

2 Answers 2

12
$\begingroup$

Also

KeySelect[StringStartsQ[Except @ "_"]][asso]

<|"key1" -> {}, "key2" -> "string"|>

$\endgroup$
1
  • $\begingroup$ That works too, thank you. $\endgroup$
    – Joe Blow
    Commented Jun 12, 2017 at 21:41
6
$\begingroup$
asso // KeyDrop@Select[Keys[asso], StringStartsQ["_"]]
$\endgroup$
2
  • $\begingroup$ My god that is complicated. $\endgroup$
    – Joe Blow
    Commented Jun 12, 2017 at 20:56
  • $\begingroup$ You're spot on with that code, thank you. $\endgroup$
    – Joe Blow
    Commented Jun 12, 2017 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.