Iterating through every other element within list recursively
I have been trying to figure out a clear and concise method to iterate through elements in a list in a form of recursive loop.
For example, if I have a list:
My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
I would like to extract every other element from the list.
New_list = [2, 6, 10, 14, 18]
I then want to take the discarded values and take every second of those. And so on, recursively.
Since my first run through the list ended at 18, I will skip over 20 and go back to the beginning of the list as I want to extract every other elements.
My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
# Elements not been used after first operation = [4, 8, 12, 16, 20]
New_list = [2, 6, 10, 14, 18, 4, 12, 20, 8, 16] # desired output
What methods can I use to loop within a list?
python python-3.x python-2.7 list recursion
add a comment |
I have been trying to figure out a clear and concise method to iterate through elements in a list in a form of recursive loop.
For example, if I have a list:
My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
I would like to extract every other element from the list.
New_list = [2, 6, 10, 14, 18]
I then want to take the discarded values and take every second of those. And so on, recursively.
Since my first run through the list ended at 18, I will skip over 20 and go back to the beginning of the list as I want to extract every other elements.
My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
# Elements not been used after first operation = [4, 8, 12, 16, 20]
New_list = [2, 6, 10, 14, 18, 4, 12, 20, 8, 16] # desired output
What methods can I use to loop within a list?
python python-3.x python-2.7 list recursion
Have you written some code that we can look at?
– L3viathan
Nov 12 at 12:18
My_list[::2]
....
– JBernardo
Nov 12 at 12:18
Do you also need to extract a list forElements not been used
? Is[2, 6, 10, 14, 18, 4, 12, 20, 16, 8]
your desired output?
– jpp
Nov 12 at 12:19
1
@jpp yes I already know how to extract every two elements but it is my goal to loop over the list and extract again from elements not been used. Indeed [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] is my desired output
– V Anon
Nov 12 at 12:24
1
@JBernardo yes I am aware of the method of extracting every other two elements, but would there be a way to loop through the list again?
– V Anon
Nov 12 at 12:25
add a comment |
I have been trying to figure out a clear and concise method to iterate through elements in a list in a form of recursive loop.
For example, if I have a list:
My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
I would like to extract every other element from the list.
New_list = [2, 6, 10, 14, 18]
I then want to take the discarded values and take every second of those. And so on, recursively.
Since my first run through the list ended at 18, I will skip over 20 and go back to the beginning of the list as I want to extract every other elements.
My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
# Elements not been used after first operation = [4, 8, 12, 16, 20]
New_list = [2, 6, 10, 14, 18, 4, 12, 20, 8, 16] # desired output
What methods can I use to loop within a list?
python python-3.x python-2.7 list recursion
I have been trying to figure out a clear and concise method to iterate through elements in a list in a form of recursive loop.
For example, if I have a list:
My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
I would like to extract every other element from the list.
New_list = [2, 6, 10, 14, 18]
I then want to take the discarded values and take every second of those. And so on, recursively.
Since my first run through the list ended at 18, I will skip over 20 and go back to the beginning of the list as I want to extract every other elements.
My_list = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
# Elements not been used after first operation = [4, 8, 12, 16, 20]
New_list = [2, 6, 10, 14, 18, 4, 12, 20, 8, 16] # desired output
What methods can I use to loop within a list?
python python-3.x python-2.7 list recursion
python python-3.x python-2.7 list recursion
edited Nov 12 at 12:44
jpp
90k2052101
90k2052101
asked Nov 12 at 12:15
V Anon
2066
2066
Have you written some code that we can look at?
– L3viathan
Nov 12 at 12:18
My_list[::2]
....
– JBernardo
Nov 12 at 12:18
Do you also need to extract a list forElements not been used
? Is[2, 6, 10, 14, 18, 4, 12, 20, 16, 8]
your desired output?
– jpp
Nov 12 at 12:19
1
@jpp yes I already know how to extract every two elements but it is my goal to loop over the list and extract again from elements not been used. Indeed [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] is my desired output
– V Anon
Nov 12 at 12:24
1
@JBernardo yes I am aware of the method of extracting every other two elements, but would there be a way to loop through the list again?
– V Anon
Nov 12 at 12:25
add a comment |
Have you written some code that we can look at?
– L3viathan
Nov 12 at 12:18
My_list[::2]
....
– JBernardo
Nov 12 at 12:18
Do you also need to extract a list forElements not been used
? Is[2, 6, 10, 14, 18, 4, 12, 20, 16, 8]
your desired output?
– jpp
Nov 12 at 12:19
1
@jpp yes I already know how to extract every two elements but it is my goal to loop over the list and extract again from elements not been used. Indeed [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] is my desired output
– V Anon
Nov 12 at 12:24
1
@JBernardo yes I am aware of the method of extracting every other two elements, but would there be a way to loop through the list again?
– V Anon
Nov 12 at 12:25
Have you written some code that we can look at?
– L3viathan
Nov 12 at 12:18
Have you written some code that we can look at?
– L3viathan
Nov 12 at 12:18
My_list[::2]
....– JBernardo
Nov 12 at 12:18
My_list[::2]
....– JBernardo
Nov 12 at 12:18
Do you also need to extract a list for
Elements not been used
? Is [2, 6, 10, 14, 18, 4, 12, 20, 16, 8]
your desired output?– jpp
Nov 12 at 12:19
Do you also need to extract a list for
Elements not been used
? Is [2, 6, 10, 14, 18, 4, 12, 20, 16, 8]
your desired output?– jpp
Nov 12 at 12:19
1
1
@jpp yes I already know how to extract every two elements but it is my goal to loop over the list and extract again from elements not been used. Indeed [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] is my desired output
– V Anon
Nov 12 at 12:24
@jpp yes I already know how to extract every two elements but it is my goal to loop over the list and extract again from elements not been used. Indeed [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] is my desired output
– V Anon
Nov 12 at 12:24
1
1
@JBernardo yes I am aware of the method of extracting every other two elements, but would there be a way to loop through the list again?
– V Anon
Nov 12 at 12:25
@JBernardo yes I am aware of the method of extracting every other two elements, but would there be a way to loop through the list again?
– V Anon
Nov 12 at 12:25
add a comment |
3 Answers
3
active
oldest
votes
Yes, you can use a while
loop with a generator:
L = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
def recursive_odds(x):
while x:
yield from x[::2]
x = x[1::2]
res = list(recursive_odds(L))
[2, 6, 10, 14, 18, 4, 12, 20, 8, 16]
add a comment |
you can use python's really cool slicing syntax:
new_list=my_list[::2]
to get every other element.
It means new_list
is my_list
from the begining to the end with a stride of 2
then your ramainder elements are elements_not_used = [item for item in my_list if item not in new_list]
and you can just continue until len(my_list)<2
This doesn't actually answer OP's question.
– jpp
Nov 12 at 12:38
add a comment |
using while loop you can do this:
final=
while len(mylist)>0:
final.extend(mylist[::2])
mylist=mylist[1::2]
print(final)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53262001%2fiterating-through-every-other-element-within-list-recursively%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yes, you can use a while
loop with a generator:
L = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
def recursive_odds(x):
while x:
yield from x[::2]
x = x[1::2]
res = list(recursive_odds(L))
[2, 6, 10, 14, 18, 4, 12, 20, 8, 16]
add a comment |
Yes, you can use a while
loop with a generator:
L = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
def recursive_odds(x):
while x:
yield from x[::2]
x = x[1::2]
res = list(recursive_odds(L))
[2, 6, 10, 14, 18, 4, 12, 20, 8, 16]
add a comment |
Yes, you can use a while
loop with a generator:
L = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
def recursive_odds(x):
while x:
yield from x[::2]
x = x[1::2]
res = list(recursive_odds(L))
[2, 6, 10, 14, 18, 4, 12, 20, 8, 16]
Yes, you can use a while
loop with a generator:
L = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
def recursive_odds(x):
while x:
yield from x[::2]
x = x[1::2]
res = list(recursive_odds(L))
[2, 6, 10, 14, 18, 4, 12, 20, 8, 16]
edited Nov 12 at 12:43
answered Nov 12 at 12:33
jpp
90k2052101
90k2052101
add a comment |
add a comment |
you can use python's really cool slicing syntax:
new_list=my_list[::2]
to get every other element.
It means new_list
is my_list
from the begining to the end with a stride of 2
then your ramainder elements are elements_not_used = [item for item in my_list if item not in new_list]
and you can just continue until len(my_list)<2
This doesn't actually answer OP's question.
– jpp
Nov 12 at 12:38
add a comment |
you can use python's really cool slicing syntax:
new_list=my_list[::2]
to get every other element.
It means new_list
is my_list
from the begining to the end with a stride of 2
then your ramainder elements are elements_not_used = [item for item in my_list if item not in new_list]
and you can just continue until len(my_list)<2
This doesn't actually answer OP's question.
– jpp
Nov 12 at 12:38
add a comment |
you can use python's really cool slicing syntax:
new_list=my_list[::2]
to get every other element.
It means new_list
is my_list
from the begining to the end with a stride of 2
then your ramainder elements are elements_not_used = [item for item in my_list if item not in new_list]
and you can just continue until len(my_list)<2
you can use python's really cool slicing syntax:
new_list=my_list[::2]
to get every other element.
It means new_list
is my_list
from the begining to the end with a stride of 2
then your ramainder elements are elements_not_used = [item for item in my_list if item not in new_list]
and you can just continue until len(my_list)<2
edited Nov 12 at 12:20
answered Nov 12 at 12:18
vencaslac
1,002217
1,002217
This doesn't actually answer OP's question.
– jpp
Nov 12 at 12:38
add a comment |
This doesn't actually answer OP's question.
– jpp
Nov 12 at 12:38
This doesn't actually answer OP's question.
– jpp
Nov 12 at 12:38
This doesn't actually answer OP's question.
– jpp
Nov 12 at 12:38
add a comment |
using while loop you can do this:
final=
while len(mylist)>0:
final.extend(mylist[::2])
mylist=mylist[1::2]
print(final)
add a comment |
using while loop you can do this:
final=
while len(mylist)>0:
final.extend(mylist[::2])
mylist=mylist[1::2]
print(final)
add a comment |
using while loop you can do this:
final=
while len(mylist)>0:
final.extend(mylist[::2])
mylist=mylist[1::2]
print(final)
using while loop you can do this:
final=
while len(mylist)>0:
final.extend(mylist[::2])
mylist=mylist[1::2]
print(final)
edited Nov 12 at 13:02
vencaslac
1,002217
1,002217
answered Nov 12 at 12:42
Gautham Mohan
213
213
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
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%2fstackoverflow.com%2fquestions%2f53262001%2fiterating-through-every-other-element-within-list-recursively%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
Have you written some code that we can look at?
– L3viathan
Nov 12 at 12:18
My_list[::2]
....– JBernardo
Nov 12 at 12:18
Do you also need to extract a list for
Elements not been used
? Is[2, 6, 10, 14, 18, 4, 12, 20, 16, 8]
your desired output?– jpp
Nov 12 at 12:19
1
@jpp yes I already know how to extract every two elements but it is my goal to loop over the list and extract again from elements not been used. Indeed [2, 6, 10, 14, 18, 4, 12, 20, 16, 8] is my desired output
– V Anon
Nov 12 at 12:24
1
@JBernardo yes I am aware of the method of extracting every other two elements, but would there be a way to loop through the list again?
– V Anon
Nov 12 at 12:25