Iterate over keys of association
$begingroup$
How do you iterate over the keys or rules of an association (in any order)? E.g. for
<|"0"->"a", "1"->"b", "2"->"c"|>
I would like to either iterate over "0"
, "1"
, "2"
or tuples containing each key and value.
For example, something that performed the equivalent of a python dictionary's items
, method would be ideal (which returns a list (or iterator) in the form [(key1, value1), ...]
, so {{"0", "a"}, {"1", "b"}, {"2", "c"}}
here).
associations
$endgroup$
add a comment |
$begingroup$
How do you iterate over the keys or rules of an association (in any order)? E.g. for
<|"0"->"a", "1"->"b", "2"->"c"|>
I would like to either iterate over "0"
, "1"
, "2"
or tuples containing each key and value.
For example, something that performed the equivalent of a python dictionary's items
, method would be ideal (which returns a list (or iterator) in the form [(key1, value1), ...]
, so {{"0", "a"}, {"1", "b"}, {"2", "c"}}
here).
associations
$endgroup$
1
$begingroup$
Simply useMap
,KeyValueMap
orKeyMap
?
$endgroup$
– Henrik Schumacher
Dec 5 '18 at 19:20
$begingroup$
Can you tell what exactly do you want to do?
$endgroup$
– Kuba♦
Dec 5 '18 at 21:42
$begingroup$
Woooops! Done! :)
$endgroup$
– SLesslyTall
Dec 6 '18 at 7:40
$begingroup$
@SLesslyTall You see, you wantKeyValueMap[List] @ asso
but who could've known :)
$endgroup$
– Kuba♦
Dec 6 '18 at 7:53
add a comment |
$begingroup$
How do you iterate over the keys or rules of an association (in any order)? E.g. for
<|"0"->"a", "1"->"b", "2"->"c"|>
I would like to either iterate over "0"
, "1"
, "2"
or tuples containing each key and value.
For example, something that performed the equivalent of a python dictionary's items
, method would be ideal (which returns a list (or iterator) in the form [(key1, value1), ...]
, so {{"0", "a"}, {"1", "b"}, {"2", "c"}}
here).
associations
$endgroup$
How do you iterate over the keys or rules of an association (in any order)? E.g. for
<|"0"->"a", "1"->"b", "2"->"c"|>
I would like to either iterate over "0"
, "1"
, "2"
or tuples containing each key and value.
For example, something that performed the equivalent of a python dictionary's items
, method would be ideal (which returns a list (or iterator) in the form [(key1, value1), ...]
, so {{"0", "a"}, {"1", "b"}, {"2", "c"}}
here).
associations
associations
edited Dec 6 '18 at 7:40
SLesslyTall
asked Dec 5 '18 at 19:15
SLesslyTallSLesslyTall
22317
22317
1
$begingroup$
Simply useMap
,KeyValueMap
orKeyMap
?
$endgroup$
– Henrik Schumacher
Dec 5 '18 at 19:20
$begingroup$
Can you tell what exactly do you want to do?
$endgroup$
– Kuba♦
Dec 5 '18 at 21:42
$begingroup$
Woooops! Done! :)
$endgroup$
– SLesslyTall
Dec 6 '18 at 7:40
$begingroup$
@SLesslyTall You see, you wantKeyValueMap[List] @ asso
but who could've known :)
$endgroup$
– Kuba♦
Dec 6 '18 at 7:53
add a comment |
1
$begingroup$
Simply useMap
,KeyValueMap
orKeyMap
?
$endgroup$
– Henrik Schumacher
Dec 5 '18 at 19:20
$begingroup$
Can you tell what exactly do you want to do?
$endgroup$
– Kuba♦
Dec 5 '18 at 21:42
$begingroup$
Woooops! Done! :)
$endgroup$
– SLesslyTall
Dec 6 '18 at 7:40
$begingroup$
@SLesslyTall You see, you wantKeyValueMap[List] @ asso
but who could've known :)
$endgroup$
– Kuba♦
Dec 6 '18 at 7:53
1
1
$begingroup$
Simply use
Map
, KeyValueMap
or KeyMap
?$endgroup$
– Henrik Schumacher
Dec 5 '18 at 19:20
$begingroup$
Simply use
Map
, KeyValueMap
or KeyMap
?$endgroup$
– Henrik Schumacher
Dec 5 '18 at 19:20
$begingroup$
Can you tell what exactly do you want to do?
$endgroup$
– Kuba♦
Dec 5 '18 at 21:42
$begingroup$
Can you tell what exactly do you want to do?
$endgroup$
– Kuba♦
Dec 5 '18 at 21:42
$begingroup$
Woooops! Done! :)
$endgroup$
– SLesslyTall
Dec 6 '18 at 7:40
$begingroup$
Woooops! Done! :)
$endgroup$
– SLesslyTall
Dec 6 '18 at 7:40
$begingroup$
@SLesslyTall You see, you want
KeyValueMap[List] @ asso
but who could've known :)$endgroup$
– Kuba♦
Dec 6 '18 at 7:53
$begingroup$
@SLesslyTall You see, you want
KeyValueMap[List] @ asso
but who could've known :)$endgroup$
– Kuba♦
Dec 6 '18 at 7:53
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
To iterate over the keys of an association, first get the keys in a list using Keys
. then use this in your favorite iteration construct (Do
, Table
, Scan
, Map
, etc):
In[16]:= assoc = <|"0" -> 0, "1" -> 1, "2" -> 2|>;
In[17]:= Do[Print[x], {x, Keys[assoc]}]
During evaluation of In[17]:= 0
During evaluation of In[17]:= 1
During evaluation of In[17]:= 2
$endgroup$
add a comment |
$begingroup$
If you would like to map over the keys to transform them into something else, you can use KeyMap
:
a = <|"0" -> 0, "1" -> 1, "2" -> 2|>;
a // KeyMap[("newKey" <> #) &]
(* <|"newKey0" -> 0, "newKey1" -> 1, "newKey2" -> 2|> *)
Otherwise, the good old Map
will iterate over the values as if the structure were a list:
a // Map[("newVal" <> ToString@#) &]
(* <|"0" -> "newVal0", "1" -> "newVal1", "2" -> "newVal2"|> *)
You should also look up KeyValueMap
to see if that would make your task easier.
Edit
Henrik Schumacher's succinct comment contains everything I have elaborated here. His comment had not been posted when I began writing this answer.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f187393%2fiterate-over-keys-of-association%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
To iterate over the keys of an association, first get the keys in a list using Keys
. then use this in your favorite iteration construct (Do
, Table
, Scan
, Map
, etc):
In[16]:= assoc = <|"0" -> 0, "1" -> 1, "2" -> 2|>;
In[17]:= Do[Print[x], {x, Keys[assoc]}]
During evaluation of In[17]:= 0
During evaluation of In[17]:= 1
During evaluation of In[17]:= 2
$endgroup$
add a comment |
$begingroup$
To iterate over the keys of an association, first get the keys in a list using Keys
. then use this in your favorite iteration construct (Do
, Table
, Scan
, Map
, etc):
In[16]:= assoc = <|"0" -> 0, "1" -> 1, "2" -> 2|>;
In[17]:= Do[Print[x], {x, Keys[assoc]}]
During evaluation of In[17]:= 0
During evaluation of In[17]:= 1
During evaluation of In[17]:= 2
$endgroup$
add a comment |
$begingroup$
To iterate over the keys of an association, first get the keys in a list using Keys
. then use this in your favorite iteration construct (Do
, Table
, Scan
, Map
, etc):
In[16]:= assoc = <|"0" -> 0, "1" -> 1, "2" -> 2|>;
In[17]:= Do[Print[x], {x, Keys[assoc]}]
During evaluation of In[17]:= 0
During evaluation of In[17]:= 1
During evaluation of In[17]:= 2
$endgroup$
To iterate over the keys of an association, first get the keys in a list using Keys
. then use this in your favorite iteration construct (Do
, Table
, Scan
, Map
, etc):
In[16]:= assoc = <|"0" -> 0, "1" -> 1, "2" -> 2|>;
In[17]:= Do[Print[x], {x, Keys[assoc]}]
During evaluation of In[17]:= 0
During evaluation of In[17]:= 1
During evaluation of In[17]:= 2
answered Dec 5 '18 at 19:41
Jason B.Jason B.
48.1k388189
48.1k388189
add a comment |
add a comment |
$begingroup$
If you would like to map over the keys to transform them into something else, you can use KeyMap
:
a = <|"0" -> 0, "1" -> 1, "2" -> 2|>;
a // KeyMap[("newKey" <> #) &]
(* <|"newKey0" -> 0, "newKey1" -> 1, "newKey2" -> 2|> *)
Otherwise, the good old Map
will iterate over the values as if the structure were a list:
a // Map[("newVal" <> ToString@#) &]
(* <|"0" -> "newVal0", "1" -> "newVal1", "2" -> "newVal2"|> *)
You should also look up KeyValueMap
to see if that would make your task easier.
Edit
Henrik Schumacher's succinct comment contains everything I have elaborated here. His comment had not been posted when I began writing this answer.
$endgroup$
add a comment |
$begingroup$
If you would like to map over the keys to transform them into something else, you can use KeyMap
:
a = <|"0" -> 0, "1" -> 1, "2" -> 2|>;
a // KeyMap[("newKey" <> #) &]
(* <|"newKey0" -> 0, "newKey1" -> 1, "newKey2" -> 2|> *)
Otherwise, the good old Map
will iterate over the values as if the structure were a list:
a // Map[("newVal" <> ToString@#) &]
(* <|"0" -> "newVal0", "1" -> "newVal1", "2" -> "newVal2"|> *)
You should also look up KeyValueMap
to see if that would make your task easier.
Edit
Henrik Schumacher's succinct comment contains everything I have elaborated here. His comment had not been posted when I began writing this answer.
$endgroup$
add a comment |
$begingroup$
If you would like to map over the keys to transform them into something else, you can use KeyMap
:
a = <|"0" -> 0, "1" -> 1, "2" -> 2|>;
a // KeyMap[("newKey" <> #) &]
(* <|"newKey0" -> 0, "newKey1" -> 1, "newKey2" -> 2|> *)
Otherwise, the good old Map
will iterate over the values as if the structure were a list:
a // Map[("newVal" <> ToString@#) &]
(* <|"0" -> "newVal0", "1" -> "newVal1", "2" -> "newVal2"|> *)
You should also look up KeyValueMap
to see if that would make your task easier.
Edit
Henrik Schumacher's succinct comment contains everything I have elaborated here. His comment had not been posted when I began writing this answer.
$endgroup$
If you would like to map over the keys to transform them into something else, you can use KeyMap
:
a = <|"0" -> 0, "1" -> 1, "2" -> 2|>;
a // KeyMap[("newKey" <> #) &]
(* <|"newKey0" -> 0, "newKey1" -> 1, "newKey2" -> 2|> *)
Otherwise, the good old Map
will iterate over the values as if the structure were a list:
a // Map[("newVal" <> ToString@#) &]
(* <|"0" -> "newVal0", "1" -> "newVal1", "2" -> "newVal2"|> *)
You should also look up KeyValueMap
to see if that would make your task easier.
Edit
Henrik Schumacher's succinct comment contains everything I have elaborated here. His comment had not been posted when I began writing this answer.
answered Dec 5 '18 at 19:25
ShredderroyShredderroy
1,5201115
1,5201115
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f187393%2fiterate-over-keys-of-association%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
Simply use
Map
,KeyValueMap
orKeyMap
?$endgroup$
– Henrik Schumacher
Dec 5 '18 at 19:20
$begingroup$
Can you tell what exactly do you want to do?
$endgroup$
– Kuba♦
Dec 5 '18 at 21:42
$begingroup$
Woooops! Done! :)
$endgroup$
– SLesslyTall
Dec 6 '18 at 7:40
$begingroup$
@SLesslyTall You see, you want
KeyValueMap[List] @ asso
but who could've known :)$endgroup$
– Kuba♦
Dec 6 '18 at 7:53