I have two lists. One with the same repeating four keys and the other with unique values. Both are the same length. I want to Associate the first list with the second list. So if that if the 5th element of the 1st and 2nd list respectively are "noun" and "house, then "house" would be added to the noun key.

wordType = {{"adjective"}, {"verb"}, {"noun"}, {"adverb"}, {"adjective"},{"verb"}}

words= {"pretty", "run", "house", "accordingly", "coarse", "catch"}

AssociationThread[wordType, words]

desired output:

 <|{"adjective"} -> "pretty", {"verb"} -> "run", {"noun"} -> "house", {"adverb"} -> "accordingly",{"adjective"} -> "coarse",{"verb"} -> "catch"|>

actual output:

<|{"adjective"} -> "coarse", {"verb"} -> "catch", {"noun"} ->  "house", {"adverb"} -> "accordingly"|>

you see the "one" key seems to be sloted. It was 1 at the begining but by the end had been replaced with 5. Same with the "two" key.

  • 6
    $\begingroup$ An association cannot have multiple instances of the same key. What you show as the desired output is not a valid association. $\endgroup$ – Szabolcs Jun 18 '16 at 21:09
  • 2
    $\begingroup$ Try Merge[Identity]@Thread@Rule[wordType, words]. $\endgroup$ – Edmund Jun 18 '16 at 21:14
  • $\begingroup$ @Edmund Brilliant! Many thanks. $\endgroup$ – Connor Jun 18 '16 at 21:52
  • 1
    $\begingroup$ If you explain what you want to use this association for, people will be able to suggest working alternatives. One is Edmund's solution. Another is Thread[wordType -> words]. What's useful really depends on what you need this for. $\endgroup$ – Szabolcs Jun 18 '16 at 21:53
  • $\begingroup$ This could likely be considered a duplicate of one of these: (60913), (4332), (61655) $\endgroup$ – Mr.Wizard Jun 19 '16 at 3:58

Associations cannot have multiple instances (Szabolcs)

Merge[Identity]@Thread@Rule[wordType, words] Works Perfectly! (Edmund)


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