How to divide one list into 2 using a For loop?

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Say I have a list called A and I want to divide it into two separate list B and C by looping through A and picking out one element to add to B and one to add to A throughout the length of A.
list-manipulation
New contributor
Bryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
2
down vote
favorite
Say I have a list called A and I want to divide it into two separate list B and C by looping through A and picking out one element to add to B and one to add to A throughout the length of A.
list-manipulation
New contributor
Bryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. You need to provide us with: the data, the criteria toSelectelements, whichCasesdefine how toSplitthe list, and most importantly: the code of what you have tried so far.
â rhermans
15 hours ago
4
A hint: the best solution will not use aForloop, but probably something in the lines ofSelect,CasesorSplit. Your question may be put on-hold because its unanswerable for lack of details. We can't help unless you edit your question to improve it and make it specific, with all the details one would need to reproduce your problem exactly. Please don't be discouraged by that cleaning-up policy. Your questions are and will be most welcomed. Learn about good questions here.
â rhermans
14 hours ago
1
AForloop is a very awkward and inefficient way to accomplish what you seek. By what criterion do you place elements into $B$ or $C$? Simple alternates?
â David G. Stork
14 hours ago
Yes, Simple alternatives or even random separation
â Bryan
14 hours ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Say I have a list called A and I want to divide it into two separate list B and C by looping through A and picking out one element to add to B and one to add to A throughout the length of A.
list-manipulation
New contributor
Bryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Say I have a list called A and I want to divide it into two separate list B and C by looping through A and picking out one element to add to B and one to add to A throughout the length of A.
list-manipulation
list-manipulation
New contributor
Bryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Bryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Bryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 15 hours ago
Bryan
191
191
New contributor
Bryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Bryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Bryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. You need to provide us with: the data, the criteria toSelectelements, whichCasesdefine how toSplitthe list, and most importantly: the code of what you have tried so far.
â rhermans
15 hours ago
4
A hint: the best solution will not use aForloop, but probably something in the lines ofSelect,CasesorSplit. Your question may be put on-hold because its unanswerable for lack of details. We can't help unless you edit your question to improve it and make it specific, with all the details one would need to reproduce your problem exactly. Please don't be discouraged by that cleaning-up policy. Your questions are and will be most welcomed. Learn about good questions here.
â rhermans
14 hours ago
1
AForloop is a very awkward and inefficient way to accomplish what you seek. By what criterion do you place elements into $B$ or $C$? Simple alternates?
â David G. Stork
14 hours ago
Yes, Simple alternatives or even random separation
â Bryan
14 hours ago
add a comment |Â
1
Welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. You need to provide us with: the data, the criteria toSelectelements, whichCasesdefine how toSplitthe list, and most importantly: the code of what you have tried so far.
â rhermans
15 hours ago
4
A hint: the best solution will not use aForloop, but probably something in the lines ofSelect,CasesorSplit. Your question may be put on-hold because its unanswerable for lack of details. We can't help unless you edit your question to improve it and make it specific, with all the details one would need to reproduce your problem exactly. Please don't be discouraged by that cleaning-up policy. Your questions are and will be most welcomed. Learn about good questions here.
â rhermans
14 hours ago
1
AForloop is a very awkward and inefficient way to accomplish what you seek. By what criterion do you place elements into $B$ or $C$? Simple alternates?
â David G. Stork
14 hours ago
Yes, Simple alternatives or even random separation
â Bryan
14 hours ago
1
1
Welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. You need to provide us with: the data, the criteria to
Select elements, which Cases define how to Split the list, and most importantly: the code of what you have tried so far.â rhermans
15 hours ago
Welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. You need to provide us with: the data, the criteria to
Select elements, which Cases define how to Split the list, and most importantly: the code of what you have tried so far.â rhermans
15 hours ago
4
4
A hint: the best solution will not use a
For loop, but probably something in the lines of Select, Cases or Split. Your question may be put on-hold because its unanswerable for lack of details. We can't help unless you edit your question to improve it and make it specific, with all the details one would need to reproduce your problem exactly. Please don't be discouraged by that cleaning-up policy. Your questions are and will be most welcomed. Learn about good questions here.â rhermans
14 hours ago
A hint: the best solution will not use a
For loop, but probably something in the lines of Select, Cases or Split. Your question may be put on-hold because its unanswerable for lack of details. We can't help unless you edit your question to improve it and make it specific, with all the details one would need to reproduce your problem exactly. Please don't be discouraged by that cleaning-up policy. Your questions are and will be most welcomed. Learn about good questions here.â rhermans
14 hours ago
1
1
A
For loop is a very awkward and inefficient way to accomplish what you seek. By what criterion do you place elements into $B$ or $C$? Simple alternates?â David G. Stork
14 hours ago
A
For loop is a very awkward and inefficient way to accomplish what you seek. By what criterion do you place elements into $B$ or $C$? Simple alternates?â David G. Stork
14 hours ago
Yes, Simple alternatives or even random separation
â Bryan
14 hours ago
Yes, Simple alternatives or even random separation
â Bryan
14 hours ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
6
down vote
Don't use For. Really. Never.
There are much more elegant ways, for example
a = Range[20];
b, c = Transpose[Partition[a, 2]]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
or (should be the fastest)
b = a[[1;;;;2]]
c = a[[2;;;;2]]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19
2, 4, 6, 8, 10, 12, 14, 16, 18, 20
or
b, c = GatherBy[a, OddQ]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
or
b = Select[a, OddQ]
c = Select[a, EvenQ]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19
2, 4, 6, 8, 10, 12, 14, 16, 18, 20
In the last two examples, you may replace OddQ and EvenQ by any other function that returns True or False in order to realize other decision rules.
Only the middle approach is robust. +1 for it.
â Bob Hanlon
9 hours ago
add a comment |Â
up vote
4
down vote
As it turns out, there is a special function for taking every other element called DownSample:
list = Range[20];
a = Downsample[list, 2]
b = Downsample[list, 2, 2]
a has all the odd values, b has all the evens.
add a comment |Â
up vote
2
down vote
list = Range@20;
here are alternatives
TakeDrop[list, 1, -1, 2]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
and here is random separation
d = RandomSample[s, Length@list/2], Complement[list, d]
4, 12, 5, 19, 3, 11, 17, 16, 6, 10, 1, 2, 7, 8, 9, 13, 14, 15, 18,
20
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
Don't use For. Really. Never.
There are much more elegant ways, for example
a = Range[20];
b, c = Transpose[Partition[a, 2]]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
or (should be the fastest)
b = a[[1;;;;2]]
c = a[[2;;;;2]]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19
2, 4, 6, 8, 10, 12, 14, 16, 18, 20
or
b, c = GatherBy[a, OddQ]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
or
b = Select[a, OddQ]
c = Select[a, EvenQ]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19
2, 4, 6, 8, 10, 12, 14, 16, 18, 20
In the last two examples, you may replace OddQ and EvenQ by any other function that returns True or False in order to realize other decision rules.
Only the middle approach is robust. +1 for it.
â Bob Hanlon
9 hours ago
add a comment |Â
up vote
6
down vote
Don't use For. Really. Never.
There are much more elegant ways, for example
a = Range[20];
b, c = Transpose[Partition[a, 2]]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
or (should be the fastest)
b = a[[1;;;;2]]
c = a[[2;;;;2]]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19
2, 4, 6, 8, 10, 12, 14, 16, 18, 20
or
b, c = GatherBy[a, OddQ]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
or
b = Select[a, OddQ]
c = Select[a, EvenQ]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19
2, 4, 6, 8, 10, 12, 14, 16, 18, 20
In the last two examples, you may replace OddQ and EvenQ by any other function that returns True or False in order to realize other decision rules.
Only the middle approach is robust. +1 for it.
â Bob Hanlon
9 hours ago
add a comment |Â
up vote
6
down vote
up vote
6
down vote
Don't use For. Really. Never.
There are much more elegant ways, for example
a = Range[20];
b, c = Transpose[Partition[a, 2]]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
or (should be the fastest)
b = a[[1;;;;2]]
c = a[[2;;;;2]]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19
2, 4, 6, 8, 10, 12, 14, 16, 18, 20
or
b, c = GatherBy[a, OddQ]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
or
b = Select[a, OddQ]
c = Select[a, EvenQ]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19
2, 4, 6, 8, 10, 12, 14, 16, 18, 20
In the last two examples, you may replace OddQ and EvenQ by any other function that returns True or False in order to realize other decision rules.
Don't use For. Really. Never.
There are much more elegant ways, for example
a = Range[20];
b, c = Transpose[Partition[a, 2]]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
or (should be the fastest)
b = a[[1;;;;2]]
c = a[[2;;;;2]]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19
2, 4, 6, 8, 10, 12, 14, 16, 18, 20
or
b, c = GatherBy[a, OddQ]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
or
b = Select[a, OddQ]
c = Select[a, EvenQ]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19
2, 4, 6, 8, 10, 12, 14, 16, 18, 20
In the last two examples, you may replace OddQ and EvenQ by any other function that returns True or False in order to realize other decision rules.
edited 11 mins ago
answered 14 hours ago
Henrik Schumacher
43.5k263129
43.5k263129
Only the middle approach is robust. +1 for it.
â Bob Hanlon
9 hours ago
add a comment |Â
Only the middle approach is robust. +1 for it.
â Bob Hanlon
9 hours ago
Only the middle approach is robust. +1 for it.
â Bob Hanlon
9 hours ago
Only the middle approach is robust. +1 for it.
â Bob Hanlon
9 hours ago
add a comment |Â
up vote
4
down vote
As it turns out, there is a special function for taking every other element called DownSample:
list = Range[20];
a = Downsample[list, 2]
b = Downsample[list, 2, 2]
a has all the odd values, b has all the evens.
add a comment |Â
up vote
4
down vote
As it turns out, there is a special function for taking every other element called DownSample:
list = Range[20];
a = Downsample[list, 2]
b = Downsample[list, 2, 2]
a has all the odd values, b has all the evens.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
As it turns out, there is a special function for taking every other element called DownSample:
list = Range[20];
a = Downsample[list, 2]
b = Downsample[list, 2, 2]
a has all the odd values, b has all the evens.
As it turns out, there is a special function for taking every other element called DownSample:
list = Range[20];
a = Downsample[list, 2]
b = Downsample[list, 2, 2]
a has all the odd values, b has all the evens.
answered 14 hours ago
bill s
52k375146
52k375146
add a comment |Â
add a comment |Â
up vote
2
down vote
list = Range@20;
here are alternatives
TakeDrop[list, 1, -1, 2]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
and here is random separation
d = RandomSample[s, Length@list/2], Complement[list, d]
4, 12, 5, 19, 3, 11, 17, 16, 6, 10, 1, 2, 7, 8, 9, 13, 14, 15, 18,
20
add a comment |Â
up vote
2
down vote
list = Range@20;
here are alternatives
TakeDrop[list, 1, -1, 2]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
and here is random separation
d = RandomSample[s, Length@list/2], Complement[list, d]
4, 12, 5, 19, 3, 11, 17, 16, 6, 10, 1, 2, 7, 8, 9, 13, 14, 15, 18,
20
add a comment |Â
up vote
2
down vote
up vote
2
down vote
list = Range@20;
here are alternatives
TakeDrop[list, 1, -1, 2]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
and here is random separation
d = RandomSample[s, Length@list/2], Complement[list, d]
4, 12, 5, 19, 3, 11, 17, 16, 6, 10, 1, 2, 7, 8, 9, 13, 14, 15, 18,
20
list = Range@20;
here are alternatives
TakeDrop[list, 1, -1, 2]
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18,
20
and here is random separation
d = RandomSample[s, Length@list/2], Complement[list, d]
4, 12, 5, 19, 3, 11, 17, 16, 6, 10, 1, 2, 7, 8, 9, 13, 14, 15, 18,
20
answered 11 hours ago
J42161217
2,257218
2,257218
add a comment |Â
add a comment |Â
Bryan is a new contributor. Be nice, and check out our Code of Conduct.
Bryan is a new contributor. Be nice, and check out our Code of Conduct.
Bryan is a new contributor. Be nice, and check out our Code of Conduct.
Bryan is a new contributor. Be nice, and check out our Code of Conduct.
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185103%2fhow-to-divide-one-list-into-2-using-a-for-loop%23new-answer', 'question_page');
);
Post as a guest
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
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
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

1
Welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. You need to provide us with: the data, the criteria to
Selectelements, whichCasesdefine how toSplitthe list, and most importantly: the code of what you have tried so far.â rhermans
15 hours ago
4
A hint: the best solution will not use a
Forloop, but probably something in the lines ofSelect,CasesorSplit. Your question may be put on-hold because its unanswerable for lack of details. We can't help unless you edit your question to improve it and make it specific, with all the details one would need to reproduce your problem exactly. Please don't be discouraged by that cleaning-up policy. Your questions are and will be most welcomed. Learn about good questions here.â rhermans
14 hours ago
1
A
Forloop is a very awkward and inefficient way to accomplish what you seek. By what criterion do you place elements into $B$ or $C$? Simple alternates?â David G. Stork
14 hours ago
Yes, Simple alternatives or even random separation
â Bryan
14 hours ago