<|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>[[1]]
gives values
1
while
<|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>[[1;;2]]
gives
<|"a" -> 1, "b" -> 2|>
I just feel an inconsistency in this kind of Part
design.
Why
<|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>[[1]]
doesn't give
"a" -> 1
as
{"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4}[[1]]
gives
"a" -> 1
or as many people point out that functions act transparently on Association
's values, why doesn't
<|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>[[1;;2]]
give
{1,2}
??? Can somebody give an explanation?
(key->value)
, one must use[[n;;n]]
syntax or[[n;;]]
. But if you use[[n]]
this will only give the value at partn
. The reason why[[n;;m]]
work for bothkey,values
at bothn
throughm
parts, is because of the;;
usage. So to pull both key+value, use;;
, else it will default to only the value at that part. $\endgroup$ – Nasser Dec 13 '14 at 4:55