Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

So I have this question, using a toy example:



the result will be 2. However what I want to obtain is "that" (or at least "b"). How do I do this without making dozens of "If"s and conditionals??

share|improve this question

marked as duplicate by Mr.Wizard Dec 6 '13 at 21:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

a = {1, "this"}; b = {2, "that"}; #[[Ordering[#, -1, #[[1]] &]]] &@{a, b}? – Dr. belisarius Dec 6 '13 at 20:43
thank you so much, pretty nice one! – Strumillo Dec 6 '13 at 21:19
Shorter a = {1, "this"}; b = {2, "that"}; #[[Ordering[#, -1]]] &@{a, b} – Dr. belisarius Dec 6 '13 at 21:25
I have marked this as a duplicate. I feel that the subject is already well covered there. (And several other places.) Please review the answers there; if afterward you disagree vote or flag to reopen. – Mr.Wizard Dec 6 '13 at 21:27
Mods should have a way to vote normally – Dr. belisarius Dec 6 '13 at 21:34

If you have Mathematica on the Raspberry Pi (or Mathematica 10), you could use Association to store your data, which allows you to do these operations easily:

With[{a = <|1 -> "this", 2 -> "that"|>},
(* that *)

You can convert your list to an association as: Association @@ Rule @@@ {a, b}

share|improve this answer
Show a screen capture, or it didn't happen – Dr. belisarius Dec 6 '13 at 21:15
@belisarius Now vote! :D – R. M. Dec 6 '13 at 21:16
Ok, Ok, you have it :) – Dr. belisarius Dec 6 '13 at 21:19

Max[0, a[[1]], b[[1]]] /. Rule @@@ {a, b}

This gives :


share|improve this answer
wooooooow, thank you! – Strumillo Dec 6 '13 at 20:57
can I ask one more thing? how do I get whole b back? I'm just mesmerized by 3 @@@ and how it works:) – Strumillo Dec 6 '13 at 20:59
To obtain {2,"that"} instead of "that", you can do Max[0, a[[1]], b[[1]]] /. ((#[[1]] -> #) & /@ {a, b}) – andre Dec 6 '13 at 21:04

Cases[{a, b}, {key_, value_} /; (key == Max[{a, b}[[All, 1]]])]


Select[{a, b}, (#[[1]] == Max[{a, b}[[All, 1]]] &)]

For variety's sake. Likely not the way to go for long lists though.

share|improve this answer

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