Range list for every item in list loop
![Multi tool use Multi tool use](http://sgv.ssvwv.com/sg/ssvwvcomimagb.png)
Multi tool use
I'm trying to create a list of ranges from a flat list of numbers.
It's working when it's looping via simple range but when trying to loop a custom list its giving empty sublists. I just started my python adventure, don't be cruel ;) Any help would be very greatly appreciated.
Expected output is from list [0, 1, 2]
-> [[0], [0, 1], [0, 1, 2]]
a = [1,2]
b =
def makerange(n):
b.append(list(range(0, n, 1)))
for a in range(10):
makerange(a)
print(b)
[, [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8]]
python function loops range
|
show 1 more comment
I'm trying to create a list of ranges from a flat list of numbers.
It's working when it's looping via simple range but when trying to loop a custom list its giving empty sublists. I just started my python adventure, don't be cruel ;) Any help would be very greatly appreciated.
Expected output is from list [0, 1, 2]
-> [[0], [0, 1], [0, 1, 2]]
a = [1,2]
b =
def makerange(n):
b.append(list(range(0, n, 1)))
for a in range(10):
makerange(a)
print(b)
[, [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8]]
python function loops range
1
It would be great if you could include the expected output.
– Daniel Mesejo
Jan 2 at 15:06
1
You defineda
as[1, 2]
, then throw it away and usea
as0
through9
from arange
. Did you want it to be based on[1, 2]
, orrange(10)
? Either way, you need to avoid reusing variable names since it's just going to confuse you; changing the loop variable toi
(so it's eitherfor i in range(10):
orfor i in a:
, thenmakerange(i)
will be a lot less confusing.
– ShadowRanger
Jan 2 at 15:07
cannot reproduce:[, [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8]]
– Jean-François Fabre
Jan 2 at 15:07
btw:b.append(list(range(0, n, 1)))
=>b.append(list(range(n)))
– Jean-François Fabre
Jan 2 at 15:08
1
Expected output done and the result is now correct
– Maciej Radwański
Jan 2 at 15:13
|
show 1 more comment
I'm trying to create a list of ranges from a flat list of numbers.
It's working when it's looping via simple range but when trying to loop a custom list its giving empty sublists. I just started my python adventure, don't be cruel ;) Any help would be very greatly appreciated.
Expected output is from list [0, 1, 2]
-> [[0], [0, 1], [0, 1, 2]]
a = [1,2]
b =
def makerange(n):
b.append(list(range(0, n, 1)))
for a in range(10):
makerange(a)
print(b)
[, [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8]]
python function loops range
I'm trying to create a list of ranges from a flat list of numbers.
It's working when it's looping via simple range but when trying to loop a custom list its giving empty sublists. I just started my python adventure, don't be cruel ;) Any help would be very greatly appreciated.
Expected output is from list [0, 1, 2]
-> [[0], [0, 1], [0, 1, 2]]
a = [1,2]
b =
def makerange(n):
b.append(list(range(0, n, 1)))
for a in range(10):
makerange(a)
print(b)
[, [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8]]
python function loops range
python function loops range
edited Jan 2 at 15:20
![](https://i.stack.imgur.com/4xczZ.jpg?s=32&g=1)
![](https://i.stack.imgur.com/4xczZ.jpg?s=32&g=1)
Jean-François Fabre
106k957115
106k957115
asked Jan 2 at 15:04
Maciej RadwańskiMaciej Radwański
42
42
1
It would be great if you could include the expected output.
– Daniel Mesejo
Jan 2 at 15:06
1
You defineda
as[1, 2]
, then throw it away and usea
as0
through9
from arange
. Did you want it to be based on[1, 2]
, orrange(10)
? Either way, you need to avoid reusing variable names since it's just going to confuse you; changing the loop variable toi
(so it's eitherfor i in range(10):
orfor i in a:
, thenmakerange(i)
will be a lot less confusing.
– ShadowRanger
Jan 2 at 15:07
cannot reproduce:[, [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8]]
– Jean-François Fabre
Jan 2 at 15:07
btw:b.append(list(range(0, n, 1)))
=>b.append(list(range(n)))
– Jean-François Fabre
Jan 2 at 15:08
1
Expected output done and the result is now correct
– Maciej Radwański
Jan 2 at 15:13
|
show 1 more comment
1
It would be great if you could include the expected output.
– Daniel Mesejo
Jan 2 at 15:06
1
You defineda
as[1, 2]
, then throw it away and usea
as0
through9
from arange
. Did you want it to be based on[1, 2]
, orrange(10)
? Either way, you need to avoid reusing variable names since it's just going to confuse you; changing the loop variable toi
(so it's eitherfor i in range(10):
orfor i in a:
, thenmakerange(i)
will be a lot less confusing.
– ShadowRanger
Jan 2 at 15:07
cannot reproduce:[, [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8]]
– Jean-François Fabre
Jan 2 at 15:07
btw:b.append(list(range(0, n, 1)))
=>b.append(list(range(n)))
– Jean-François Fabre
Jan 2 at 15:08
1
Expected output done and the result is now correct
– Maciej Radwański
Jan 2 at 15:13
1
1
It would be great if you could include the expected output.
– Daniel Mesejo
Jan 2 at 15:06
It would be great if you could include the expected output.
– Daniel Mesejo
Jan 2 at 15:06
1
1
You defined
a
as [1, 2]
, then throw it away and use a
as 0
through 9
from a range
. Did you want it to be based on [1, 2]
, or range(10)
? Either way, you need to avoid reusing variable names since it's just going to confuse you; changing the loop variable to i
(so it's either for i in range(10):
or for i in a:
, then makerange(i)
will be a lot less confusing.– ShadowRanger
Jan 2 at 15:07
You defined
a
as [1, 2]
, then throw it away and use a
as 0
through 9
from a range
. Did you want it to be based on [1, 2]
, or range(10)
? Either way, you need to avoid reusing variable names since it's just going to confuse you; changing the loop variable to i
(so it's either for i in range(10):
or for i in a:
, then makerange(i)
will be a lot less confusing.– ShadowRanger
Jan 2 at 15:07
cannot reproduce:
[, [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8]]
– Jean-François Fabre
Jan 2 at 15:07
cannot reproduce:
[, [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8]]
– Jean-François Fabre
Jan 2 at 15:07
btw:
b.append(list(range(0, n, 1)))
=> b.append(list(range(n)))
– Jean-François Fabre
Jan 2 at 15:08
btw:
b.append(list(range(0, n, 1)))
=> b.append(list(range(n)))
– Jean-François Fabre
Jan 2 at 15:08
1
1
Expected output done and the result is now correct
– Maciej Radwański
Jan 2 at 15:13
Expected output done and the result is now correct
– Maciej Radwański
Jan 2 at 15:13
|
show 1 more comment
3 Answers
3
active
oldest
votes
Don't overcomplicate this:
>>> a = [0,1,2]
>>> [list(range(n+1)) for n in a]
[[0], [0, 1], [0, 1, 2]]
Adding 1 to range
endpoint to include the end value.
add a comment |
It is safer to keep the colleting list inside the function and return it from the function:
def multiranges(data):
rv =
for p in data:
rv.append(list(range(p+1)))
return rv
print(multiranges([0,1,2]))
Output:
[[0],[0,1],[0,1,2]]
add a comment |
Use slicing in case your list isn't integers:
- Case 1:
With strings
l = [*'ABCDEF']
[l[:n+1] for n in range(len(l))]
Output:
[['A'],
['A', 'B'],
['A', 'B', 'C'],
['A', 'B', 'C', 'D'],
['A', 'B', 'C', 'D', 'E'],
['A', 'B', 'C', 'D', 'E', 'F']]
- Case 2:
And with l = [0,1,2]
l = [0,1,2]
[l[:n+1] for n in range(len(l))]
Output:
[[0], [0, 1], [0, 1, 2]]
- Case 3
Or list of integers are in non ascending order:
l = [2,1,0]
[l[:n+1] for n in range(len(l))]
Output:
[[2], [2, 1], [2, 1, 0]]
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%2f54008624%2frange-list-for-every-item-in-list-loop%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
Don't overcomplicate this:
>>> a = [0,1,2]
>>> [list(range(n+1)) for n in a]
[[0], [0, 1], [0, 1, 2]]
Adding 1 to range
endpoint to include the end value.
add a comment |
Don't overcomplicate this:
>>> a = [0,1,2]
>>> [list(range(n+1)) for n in a]
[[0], [0, 1], [0, 1, 2]]
Adding 1 to range
endpoint to include the end value.
add a comment |
Don't overcomplicate this:
>>> a = [0,1,2]
>>> [list(range(n+1)) for n in a]
[[0], [0, 1], [0, 1, 2]]
Adding 1 to range
endpoint to include the end value.
Don't overcomplicate this:
>>> a = [0,1,2]
>>> [list(range(n+1)) for n in a]
[[0], [0, 1], [0, 1, 2]]
Adding 1 to range
endpoint to include the end value.
answered Jan 2 at 15:10
![](https://i.stack.imgur.com/4xczZ.jpg?s=32&g=1)
![](https://i.stack.imgur.com/4xczZ.jpg?s=32&g=1)
Jean-François FabreJean-François Fabre
106k957115
106k957115
add a comment |
add a comment |
It is safer to keep the colleting list inside the function and return it from the function:
def multiranges(data):
rv =
for p in data:
rv.append(list(range(p+1)))
return rv
print(multiranges([0,1,2]))
Output:
[[0],[0,1],[0,1,2]]
add a comment |
It is safer to keep the colleting list inside the function and return it from the function:
def multiranges(data):
rv =
for p in data:
rv.append(list(range(p+1)))
return rv
print(multiranges([0,1,2]))
Output:
[[0],[0,1],[0,1,2]]
add a comment |
It is safer to keep the colleting list inside the function and return it from the function:
def multiranges(data):
rv =
for p in data:
rv.append(list(range(p+1)))
return rv
print(multiranges([0,1,2]))
Output:
[[0],[0,1],[0,1,2]]
It is safer to keep the colleting list inside the function and return it from the function:
def multiranges(data):
rv =
for p in data:
rv.append(list(range(p+1)))
return rv
print(multiranges([0,1,2]))
Output:
[[0],[0,1],[0,1,2]]
answered Jan 2 at 15:16
![](https://i.stack.imgur.com/4t4nq.jpg?s=32&g=1)
![](https://i.stack.imgur.com/4t4nq.jpg?s=32&g=1)
Patrick ArtnerPatrick Artner
25.3k62544
25.3k62544
add a comment |
add a comment |
Use slicing in case your list isn't integers:
- Case 1:
With strings
l = [*'ABCDEF']
[l[:n+1] for n in range(len(l))]
Output:
[['A'],
['A', 'B'],
['A', 'B', 'C'],
['A', 'B', 'C', 'D'],
['A', 'B', 'C', 'D', 'E'],
['A', 'B', 'C', 'D', 'E', 'F']]
- Case 2:
And with l = [0,1,2]
l = [0,1,2]
[l[:n+1] for n in range(len(l))]
Output:
[[0], [0, 1], [0, 1, 2]]
- Case 3
Or list of integers are in non ascending order:
l = [2,1,0]
[l[:n+1] for n in range(len(l))]
Output:
[[2], [2, 1], [2, 1, 0]]
add a comment |
Use slicing in case your list isn't integers:
- Case 1:
With strings
l = [*'ABCDEF']
[l[:n+1] for n in range(len(l))]
Output:
[['A'],
['A', 'B'],
['A', 'B', 'C'],
['A', 'B', 'C', 'D'],
['A', 'B', 'C', 'D', 'E'],
['A', 'B', 'C', 'D', 'E', 'F']]
- Case 2:
And with l = [0,1,2]
l = [0,1,2]
[l[:n+1] for n in range(len(l))]
Output:
[[0], [0, 1], [0, 1, 2]]
- Case 3
Or list of integers are in non ascending order:
l = [2,1,0]
[l[:n+1] for n in range(len(l))]
Output:
[[2], [2, 1], [2, 1, 0]]
add a comment |
Use slicing in case your list isn't integers:
- Case 1:
With strings
l = [*'ABCDEF']
[l[:n+1] for n in range(len(l))]
Output:
[['A'],
['A', 'B'],
['A', 'B', 'C'],
['A', 'B', 'C', 'D'],
['A', 'B', 'C', 'D', 'E'],
['A', 'B', 'C', 'D', 'E', 'F']]
- Case 2:
And with l = [0,1,2]
l = [0,1,2]
[l[:n+1] for n in range(len(l))]
Output:
[[0], [0, 1], [0, 1, 2]]
- Case 3
Or list of integers are in non ascending order:
l = [2,1,0]
[l[:n+1] for n in range(len(l))]
Output:
[[2], [2, 1], [2, 1, 0]]
Use slicing in case your list isn't integers:
- Case 1:
With strings
l = [*'ABCDEF']
[l[:n+1] for n in range(len(l))]
Output:
[['A'],
['A', 'B'],
['A', 'B', 'C'],
['A', 'B', 'C', 'D'],
['A', 'B', 'C', 'D', 'E'],
['A', 'B', 'C', 'D', 'E', 'F']]
- Case 2:
And with l = [0,1,2]
l = [0,1,2]
[l[:n+1] for n in range(len(l))]
Output:
[[0], [0, 1], [0, 1, 2]]
- Case 3
Or list of integers are in non ascending order:
l = [2,1,0]
[l[:n+1] for n in range(len(l))]
Output:
[[2], [2, 1], [2, 1, 0]]
edited Jan 2 at 16:03
answered Jan 2 at 15:20
![](https://i.stack.imgur.com/jOvgu.png?s=32&g=1)
![](https://i.stack.imgur.com/jOvgu.png?s=32&g=1)
Scott BostonScott Boston
56.7k73158
56.7k73158
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.
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%2f54008624%2frange-list-for-every-item-in-list-loop%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
mB7UVw1Gr LNlEQTPJUD0OH6CsG,noYipj5rZ
1
It would be great if you could include the expected output.
– Daniel Mesejo
Jan 2 at 15:06
1
You defined
a
as[1, 2]
, then throw it away and usea
as0
through9
from arange
. Did you want it to be based on[1, 2]
, orrange(10)
? Either way, you need to avoid reusing variable names since it's just going to confuse you; changing the loop variable toi
(so it's eitherfor i in range(10):
orfor i in a:
, thenmakerange(i)
will be a lot less confusing.– ShadowRanger
Jan 2 at 15:07
cannot reproduce:
[, [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8]]
– Jean-François Fabre
Jan 2 at 15:07
btw:
b.append(list(range(0, n, 1)))
=>b.append(list(range(n)))
– Jean-François Fabre
Jan 2 at 15:08
1
Expected output done and the result is now correct
– Maciej Radwański
Jan 2 at 15:13