as = <|1 -> 2.2, 2 -> 3.4, 3 -> 8.1|>;
I don't understand the following:
as[[1]]
2.2
But I expected to see
<|1 -> 2.2|>
On the other hand:
as[[1 ;; 1]]
<|1 -> 2.2|>
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityProbably should be handled by KeyTake
:
KeyTake[as, 1]
<|1->2.2|>
Also works with multiple "selections":
KeyTake[as, {1, 3}]
<|1 -> 2.2, 3 -> 8.1|>
The difference could be related to the fact that Part
(as[[1]]
) treats associations transparently while as[[1;;1]]
(i.e. as[[1~Span~1]]
) is equivalent to Take[as[1,1]]
, and Take
does not treat associations transparently.
as[[{1}]]
and if you have other head than association, it will be kept too. $\endgroup$list[[1]]
you don't expect to get a list so why do you expectassociation[[1]]
to give an association? $\endgroup$1->2.2
$\endgroup$