Iterate over keys of association
up vote
1
down vote
favorite
How do you iterate over the keys or rules of an association (in any order)? E.g. for
<|"0"->0, "1"->1, "2"->2|>
I would like to either iterate over "0", "1", "2".
associations
add a comment |
up vote
1
down vote
favorite
How do you iterate over the keys or rules of an association (in any order)? E.g. for
<|"0"->0, "1"->1, "2"->2|>
I would like to either iterate over "0", "1", "2".
associations
1
Simply useMap,KeyValueMaporKeyMap?
– Henrik Schumacher
1 hour ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
How do you iterate over the keys or rules of an association (in any order)? E.g. for
<|"0"->0, "1"->1, "2"->2|>
I would like to either iterate over "0", "1", "2".
associations
How do you iterate over the keys or rules of an association (in any order)? E.g. for
<|"0"->0, "1"->1, "2"->2|>
I would like to either iterate over "0", "1", "2".
associations
associations
asked 2 hours ago
SLesslyTall
21316
21316
1
Simply useMap,KeyValueMaporKeyMap?
– Henrik Schumacher
1 hour ago
add a comment |
1
Simply useMap,KeyValueMaporKeyMap?
– Henrik Schumacher
1 hour ago
1
1
Simply use
Map, KeyValueMap or KeyMap?– Henrik Schumacher
1 hour ago
Simply use
Map, KeyValueMap or KeyMap?– Henrik Schumacher
1 hour ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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
add a comment |
up vote
4
down vote
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.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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
add a comment |
up vote
1
down vote
accepted
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
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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
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 1 hour ago
Jason B.
47.4k387185
47.4k387185
add a comment |
add a comment |
up vote
4
down vote
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.
add a comment |
up vote
4
down vote
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.
add a comment |
up vote
4
down vote
up vote
4
down vote
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.
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 1 hour ago
Shredderroy
1,2731014
1,2731014
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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
Simply use
Map,KeyValueMaporKeyMap?– Henrik Schumacher
1 hour ago