Subject: Wolfram Language question

From: Bob Doe <[email-masked]>
Date: Fri, 13 May 2016 13:50:33 -0500

Alice,

Which function should I be using if I want to have a key-value pair?

Bob

---

From: Alice Johnson <[email-masked]>
Date: Fri, 13 May 2016 13:52:15 -0500

> Which function should I be using if I want to have a key-value pair?
You can use Association to do that.

Alice

---

From: Bob Doe <[email-masked]>
Date: Fri, 13 May 2016 13:56:20 -0500

Great.  This is exactly what I am looking for.
I have a couple more questions for you.
If I have an association assoc = <|"a" -> 1, "b" -> 2, "c" -> 3|>, how would I add another key-value pair, like "d" -> 4, to the existing association?
Also I noticed if I try to get the value assoc["z"], it returns Missing["KeyAbsent"]. Is there any way I could instead get the value 0 if the key doesn't exist?

---

From: Bob Doe <[email-masked]>
Date: Fri, 13 May 2016 13:58:45 -0500

Thanks,

Bob

---

From: Alice Johnson <[email-masked]>
Date: Fri, 13 May 2016 14:01:27 -0500

Glad that I can help! CCing Chuck so he can see this example.

> If I have an association assoc = <|"a" -> 1, "b" -> 2, "c" -> 3|>, how would I add another key-value pair, like "d" -> 4, to the existing association?
The function AssociateTo can add a key-value pair to an association in 
place. For example, you could do AssociateTo[assoc, {"d" -> 4, "e" -> 
5}], and assoc would now be <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4, 
"e" -> 5|>.
> Also I noticed if I try to get the value assoc["z"], it returns Missing["KeyAbsent"]. Is there any way I could instead get the value 0 if the key doesn't exist?
There is another function called Lookup which returns the value 
associated with a key if it exists, and takes an optional third 
parameter to define a default return value.
Lookup[assoc, "z", 0] should do what you're looking for.