Range list for every item in list loop

Multi tool use
Multi tool use












0















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]]









share|improve this question




















  • 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 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











  • 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
















0















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]]









share|improve this question




















  • 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 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











  • 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














0












0








0








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]]









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 15:20









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 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











  • 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





    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 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











  • 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












3 Answers
3






active

oldest

votes


















3














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.






share|improve this answer































    0














    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]]





    share|improve this answer































      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]]





      share|improve this answer

























        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        3














        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.






        share|improve this answer




























          3














          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.






          share|improve this answer


























            3












            3








            3







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 2 at 15:10









            Jean-François FabreJean-François Fabre

            106k957115




            106k957115

























                0














                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]]





                share|improve this answer




























                  0














                  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]]





                  share|improve this answer


























                    0












                    0








                    0







                    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]]





                    share|improve this answer













                    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]]






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 2 at 15:16









                    Patrick ArtnerPatrick Artner

                    25.3k62544




                    25.3k62544























                        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]]





                        share|improve this answer






























                          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]]





                          share|improve this answer




























                            0












                            0








                            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]]





                            share|improve this answer















                            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]]






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 2 at 16:03

























                            answered Jan 2 at 15:20









                            Scott BostonScott Boston

                            56.7k73158




                            56.7k73158






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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
                                IoXwR4Qj9H

                                Popular posts from this blog

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas