Message Boards Message Boards

1
|
2947 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:
GROUPS:

Association from key-value list

Posted 10 years ago

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}};
KeyValListToAssociation[list2d]
POSTED BY: Jesse Friedman
2 Replies

Cool.

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
Attachments
Remove
or Discard

Group Abstract Group Abstract