Message Boards Message Boards


Association from key-value list

Posted 9 years ago
2 Replies
2 Total Likes

Before associations, I dealt quite a bit with key-value lists. AssociationThread makes an association from a list of keys and a separate list of values, but I couldn't find any function that does the same with a single list, so I made one. It's quite simple, but I figure someone might find it helpful.

KeyValListToAssociation[list_] :=
  AssociationThread @@ Transpose@list;
KeyValListToAssociation::usage = "Makes an association from a list of the form {{key1,value1},{key2,value2},{key3,value3}}";

list2d = {{a, 1}, {b, 2}, {c, 3}, {d, 4}};
POSTED BY: Jesse Friedman
2 Replies


Small typo, I think you meant:

KeyValListToAssociation[list_] :=
  AssociationThread @@ Transpose@list
POSTED BY: David Reiss

You're right; I corrected it.

POSTED BY: Jesse Friedman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract