Skip to main content
29 votes

Difference between HashTable datastructure and Association datastructure

Summary Important differences between Association and CreateDataStructure["HashTable"] include: associations are ...
WReach's user avatar
  • 68.9k
28 votes
Accepted

Assign names to Dataset columns

data = N @ Normalize[#, Total] & @ Counts @ Characters @ ExampleData[ {"Text", "DeclarationOfIndependence"} ]; Dataset @ data ...
Kuba's user avatar
  • 137k
24 votes
Accepted

What's the difference between these 2 associations?

Preamble The real problem here seems somewhat deeper than what the (mostly correct) observations in comments indicate. In Mathematica, a number of objects, which are so-called raw objects (including ...
Leonid Shifrin's user avatar
23 votes

Reverse Lookup: getting keys from values

PositionIndex[assoc][<value>] For example: ...
user1066's user avatar
  • 18.4k
22 votes
Accepted

Count works differently depending on how an association was created

As Kirill Belov notes in a comment, the issue is related to the fact that the list a1 is a packed array (generated by Range) ...
WReach's user avatar
  • 68.9k
21 votes

Assign names to Dataset columns

Actully we have a easiest way,suppose you have dataset like ...
yode's user avatar
  • 26.8k
21 votes
Accepted

Is it possible to avoid unpacking when merging Association?

One can use TracePrint to see how Flatten is being used by Merge: ...
Carl Woll's user avatar
  • 131k
20 votes
Accepted

How to "flatten" a nested Association?

Another idea: FixedPoint[Association[Normal[#] /. Rule[n_, m_Association] :> KeyMap[Append[n, #] &, m]] &, KeyMap[{#} &, asso]] <...
Coolwater's user avatar
  • 20.3k
20 votes
Accepted

Convert List of Associations into Association of Lists

You can use the undocumented AllowedHeads option of Transpose to do this: ...
Carl Woll's user avatar
  • 131k
19 votes

Reverse Lookup: getting keys from values

One way would be to "invert" the association and then look up values in the result: ...
WReach's user avatar
  • 68.9k
19 votes

Bug with Block & Lookup: Scope Variable Leak

Looks like a bug to me as well. Just as Michael I would suspect that the problem is more with Lookup than Block or ...
Albert Retey's user avatar
  • 23.6k
19 votes

Merge function performing too slowly; what can be done about it?

It appears GroupBy does not suffer from this performance issue, so here is an alternative implementation using it, compared to ...
Mr.Wizard's user avatar
  • 272k
17 votes
Accepted

Pattern matching to a function evaluation inside an Association

Association is HoldAllComplete. Once it is created, its parts will then normally be held unevaluated. Use ...
Leonid Shifrin's user avatar
17 votes
Accepted

Reversing a GroupBy operation

This looks a little simpler: ...
Leonid Shifrin's user avatar
16 votes
Accepted

Min and Max of a list of Associations

How about ...
Shredderroy's user avatar
  • 5,249
16 votes

Substitute for Association inside Compile?

For what it's worth, you can work with a HashTable as long as you're willing to put in a bit of effort ...
b3m2a1's user avatar
  • 46.9k
16 votes
Accepted

Evaluated vs. unevaluated Association

Summary The confusion we observe here is largely due to ambiguous use of the symbol Association as an expression head. On the one hand, ...
WReach's user avatar
  • 68.9k
15 votes
Accepted

Reshaping associations, generalization of GroupBy

One approach is to employ a helper function that unwraps singleton lists: {delist[v_]} ^:= v With this, the GroupBy ...
WReach's user avatar
  • 68.9k
15 votes
Accepted

Merge function performing too slowly; what can be done about it?

This would seem to really be a question on runtime complexity of Merge, or more specifically in this case Merge[Total]. It seems ...
kirma's user avatar
  • 19.1k
14 votes
Accepted

Transpose fail in 11.1 Dataset

As observed in the question, the problem lies with the type information of the dataset. Specifically, the type information for the innermost association is missing (i.e. "any type"). This causes the ...
WReach's user avatar
  • 68.9k
14 votes

Move the outer key inward in an association

Here's an approach using @@@ (Apply at level {1}): ...
Lukas Lang's user avatar
14 votes
Accepted

Strange behaviour of an association

The problem seems to be the packed arrays generated by Range: ...
Sjoerd Smit's user avatar
  • 23.5k
13 votes
Accepted

Pattern matching Association in rules

The problem can be seen in simpler expressions: MatchQ[<||>, <||>] (* True *) MatchQ[<||>, HoldPattern[<||>]] (* False *) This behaviour ...
WReach's user avatar
  • 68.9k
13 votes
Accepted

Accesing keys based on position in an association

Perhaps: GetKey[assoc_, index_] := First @ Keys @ Take[assoc, {index}] Your first example: ...
Carl Woll's user avatar
  • 131k
13 votes

Is it possible to avoid unpacking when merging Association?

Instead of using Merge, you can also use AssociationTranspose which is often much faster than ...
Sjoerd Smit's user avatar
  • 23.5k
12 votes
Accepted

How to match Association[]?

To answer the question of why there is no match let's look at the output of Trace: ...
RunnyKine's user avatar
  • 33.1k
12 votes

Slicing an Association with named key span without normalizing?

I don't know if this is useful to you but it seems a little cleaner than your own code: ...
Mr.Wizard's user avatar
  • 272k
12 votes
Accepted

Remove element by Key

Also KeySelect[StringStartsQ[Except @ "_"]][asso] <|"key1" -> {}, "key2" -> "string"|>
kglr's user avatar
  • 396k
12 votes
Accepted

Map Values in 'nested' association

I have failed to find a good duplicate so: assoc // Query[All, {"a" -> f}] or MapAt[f, assoc, {All, "a"}]
Kuba's user avatar
  • 137k
12 votes
Accepted

Converting a list of associations into a single association

This is CW to record Kuba's comment which seems to be the simplest answer. list = {<|"a" -> aValue|>, <|"b" -> bValue|>}; assoc = <|list|> ...

Only top scored, non community-wiki answers of a minimum length are eligible