Loop from a specific point in a list of lists Python









up vote
1
down vote

favorite












I would like to append to a new list all elements of an existing list of lists after a specific point



m = [[1,2,3],[4,5,10],[6,2,1]]
specific point = m[0][2]
newlist = [3,4,5,10,6,2,1]









share|improve this question























  • Are sublists of equal size?
    – coldspeed
    Nov 11 at 22:29










  • Yes they are, ex. all length of 2, or all length of 5
    – Cheetaiean
    Nov 11 at 22:31















up vote
1
down vote

favorite












I would like to append to a new list all elements of an existing list of lists after a specific point



m = [[1,2,3],[4,5,10],[6,2,1]]
specific point = m[0][2]
newlist = [3,4,5,10,6,2,1]









share|improve this question























  • Are sublists of equal size?
    – coldspeed
    Nov 11 at 22:29










  • Yes they are, ex. all length of 2, or all length of 5
    – Cheetaiean
    Nov 11 at 22:31













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I would like to append to a new list all elements of an existing list of lists after a specific point



m = [[1,2,3],[4,5,10],[6,2,1]]
specific point = m[0][2]
newlist = [3,4,5,10,6,2,1]









share|improve this question















I would like to append to a new list all elements of an existing list of lists after a specific point



m = [[1,2,3],[4,5,10],[6,2,1]]
specific point = m[0][2]
newlist = [3,4,5,10,6,2,1]






python list nested-lists flatten






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 23:44









coldspeed

114k18105182




114k18105182










asked Nov 11 at 22:27









Cheetaiean

1041210




1041210











  • Are sublists of equal size?
    – coldspeed
    Nov 11 at 22:29










  • Yes they are, ex. all length of 2, or all length of 5
    – Cheetaiean
    Nov 11 at 22:31

















  • Are sublists of equal size?
    – coldspeed
    Nov 11 at 22:29










  • Yes they are, ex. all length of 2, or all length of 5
    – Cheetaiean
    Nov 11 at 22:31
















Are sublists of equal size?
– coldspeed
Nov 11 at 22:29




Are sublists of equal size?
– coldspeed
Nov 11 at 22:29












Yes they are, ex. all length of 2, or all length of 5
– Cheetaiean
Nov 11 at 22:31





Yes they are, ex. all length of 2, or all length of 5
– Cheetaiean
Nov 11 at 22:31













6 Answers
6






active

oldest

votes

















up vote
1
down vote



accepted










You can put a conditional in your iteration and only add based on that condition. Once you hit that specific index, make your condition true. Something like this:



m = [[1,2,3],[4,5,10],[6,2,1]]
specific_point = (0,2)
newlist = [3,4,5,10,6,2,1]


output =
for i in range(len(m)):
for j in range(len(m[i])):
if (i,j) < specific_point:
continue

output.append(m[i][j])


output:



[3, 4, 5, 10, 6, 2, 1]





share|improve this answer






















  • This seems like the most rudimentary way, thanks. I will double check if it works when I return home
    – Cheetaiean
    Nov 11 at 22:50






  • 1




    You can also roll this into a list-comp, eg: output = [v for y, r in enumerate(m) for x, v in enumerate(r) if (y, x) >= specific_point]...
    – Jon Clements
    Nov 11 at 22:51


















up vote
3
down vote













Here's a couple of functional approaches for efficiently iterating over your data.



If sublists are evenly sized, and you know the index from where to begin extracting elements, use chain + islice:



from itertools import chain, islice
n = 3 # Sublist size.
i,j = 0,2
newlist = list(islice(chain.from_iterable(m), i*n + j, None))



If you don't know the size of your sublists in advance, you can use next to discard the first portion of your data.



V = chain.from_iterable(m)
next(v for v in V if v == m[i][j])
newlist = list(V)
newlist.insert(m[i][j], 0)


This assumes there is no identical value earlier in the sequence.






share|improve this answer






















  • These solutions are a bit too advanced, I'm expected to use more basic code. Thank you though
    – Cheetaiean
    Nov 11 at 22:51

















up vote
2
down vote













You can directly slice off the remainder of the first target list and then add on all subsequent elements, eg:



m = [[1,2,3],[4,5,10],[6,2,1]]
y, x = 0, 2
new_list = m[y][x:] + [v for el in m[y+1:] for v in el]
# [3, 4, 5, 10, 6, 2, 1]





share|improve this answer



























    up vote
    1
    down vote













    why not flatten the initial list and go from there



    flat_list = [item for sublist in m for item in sublist]



    would return [1,2,3,4,5,10,6,2,1] so now you're really on flat_list[2:]






    share|improve this answer




















    • It is in a function in which specific_point serves as an input, i.e. def find_value(m, specific_point)
      – Cheetaiean
      Nov 11 at 22:36










    • right but once you flatten the list all you have to do is compute the first index of the slice, based on the specific point
      – vencaslac
      Nov 11 at 22:41










    • Just pointing out, this will create a new flattened list, and then flat_list[:2] will create yet another list with the sliced elements.
      – coldspeed
      Nov 11 at 22:43











    • @vencaslac You're right, but that only works while specific_point[0] is 0. How about if specific point was [2,1] for example.
      – Cheetaiean
      Nov 11 at 22:43











    • 2xlen(sublist)+1
      – vencaslac
      Nov 11 at 22:44

















    up vote
    1
    down vote













    Most of the answers only work for this specific shape of nested list, but it's also possible to create a solution that works with any shape of nested list.



    def flatten_from(sequence, path=):
    start = path.pop(0) if path else 0
    for item in sequence[start:]:
    if isinstance(item, (list, tuple)):
    yield from flatten_from(item, path)
    else:
    yield item


    With the example from the question



    >>> list(flatten_from([[1, 2, 3], [4, 5, 10], [6, 2, 1]], [0, 2]))
    [3, 4, 5, 10, 6, 2, 1]


    It also works with any shape and level of nesting of the input data



    m = [[1], [[2], [3, 4, 5, 6, 7]], 8, [9, [10, 11]]]

    flatten_from(m, )) # 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
    flatten_from(m, [2]) # 8, 9, 10, 11
    flatten_from(m, [1, 1, 3]) # 6, 7, 8, 9, 10, 11


    This is a bit of a bastard algorithm, though. On one hand, it uses nice functional programming concepts: recursion and yield.



    On the other hand it relies on the side effect of mutating the path argument with list.pop, so it's not a pure function.






    share|improve this answer





























      up vote
      0
      down vote













      Below solution will work for your case where your array is restricted to list of list and the size of 'sublist' is consistent throughout i.e "3" in your case



      m = [[1,2,3],[4,5,10],[6,2,1]] #input 2D array
      a, b = 0, 2 #user input --> specific point a and b
      flat_list_m = [item for firstlist in m for item in firstlist] #flat the 2D list
      print (flat_list_m[len(m[0])*a+b:]) #print from specific position a and b, considering your sublist length is consistent throughout.


      I hope this helps! :)






      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',
        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%2f53253871%2floop-from-a-specific-point-in-a-list-of-lists-python%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote



        accepted










        You can put a conditional in your iteration and only add based on that condition. Once you hit that specific index, make your condition true. Something like this:



        m = [[1,2,3],[4,5,10],[6,2,1]]
        specific_point = (0,2)
        newlist = [3,4,5,10,6,2,1]


        output =
        for i in range(len(m)):
        for j in range(len(m[i])):
        if (i,j) < specific_point:
        continue

        output.append(m[i][j])


        output:



        [3, 4, 5, 10, 6, 2, 1]





        share|improve this answer






















        • This seems like the most rudimentary way, thanks. I will double check if it works when I return home
          – Cheetaiean
          Nov 11 at 22:50






        • 1




          You can also roll this into a list-comp, eg: output = [v for y, r in enumerate(m) for x, v in enumerate(r) if (y, x) >= specific_point]...
          – Jon Clements
          Nov 11 at 22:51















        up vote
        1
        down vote



        accepted










        You can put a conditional in your iteration and only add based on that condition. Once you hit that specific index, make your condition true. Something like this:



        m = [[1,2,3],[4,5,10],[6,2,1]]
        specific_point = (0,2)
        newlist = [3,4,5,10,6,2,1]


        output =
        for i in range(len(m)):
        for j in range(len(m[i])):
        if (i,j) < specific_point:
        continue

        output.append(m[i][j])


        output:



        [3, 4, 5, 10, 6, 2, 1]





        share|improve this answer






















        • This seems like the most rudimentary way, thanks. I will double check if it works when I return home
          – Cheetaiean
          Nov 11 at 22:50






        • 1




          You can also roll this into a list-comp, eg: output = [v for y, r in enumerate(m) for x, v in enumerate(r) if (y, x) >= specific_point]...
          – Jon Clements
          Nov 11 at 22:51













        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        You can put a conditional in your iteration and only add based on that condition. Once you hit that specific index, make your condition true. Something like this:



        m = [[1,2,3],[4,5,10],[6,2,1]]
        specific_point = (0,2)
        newlist = [3,4,5,10,6,2,1]


        output =
        for i in range(len(m)):
        for j in range(len(m[i])):
        if (i,j) < specific_point:
        continue

        output.append(m[i][j])


        output:



        [3, 4, 5, 10, 6, 2, 1]





        share|improve this answer














        You can put a conditional in your iteration and only add based on that condition. Once you hit that specific index, make your condition true. Something like this:



        m = [[1,2,3],[4,5,10],[6,2,1]]
        specific_point = (0,2)
        newlist = [3,4,5,10,6,2,1]


        output =
        for i in range(len(m)):
        for j in range(len(m[i])):
        if (i,j) < specific_point:
        continue

        output.append(m[i][j])


        output:



        [3, 4, 5, 10, 6, 2, 1]






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 22:39

























        answered Nov 11 at 22:32









        LeKhan9

        921112




        921112











        • This seems like the most rudimentary way, thanks. I will double check if it works when I return home
          – Cheetaiean
          Nov 11 at 22:50






        • 1




          You can also roll this into a list-comp, eg: output = [v for y, r in enumerate(m) for x, v in enumerate(r) if (y, x) >= specific_point]...
          – Jon Clements
          Nov 11 at 22:51

















        • This seems like the most rudimentary way, thanks. I will double check if it works when I return home
          – Cheetaiean
          Nov 11 at 22:50






        • 1




          You can also roll this into a list-comp, eg: output = [v for y, r in enumerate(m) for x, v in enumerate(r) if (y, x) >= specific_point]...
          – Jon Clements
          Nov 11 at 22:51
















        This seems like the most rudimentary way, thanks. I will double check if it works when I return home
        – Cheetaiean
        Nov 11 at 22:50




        This seems like the most rudimentary way, thanks. I will double check if it works when I return home
        – Cheetaiean
        Nov 11 at 22:50




        1




        1




        You can also roll this into a list-comp, eg: output = [v for y, r in enumerate(m) for x, v in enumerate(r) if (y, x) >= specific_point]...
        – Jon Clements
        Nov 11 at 22:51





        You can also roll this into a list-comp, eg: output = [v for y, r in enumerate(m) for x, v in enumerate(r) if (y, x) >= specific_point]...
        – Jon Clements
        Nov 11 at 22:51













        up vote
        3
        down vote













        Here's a couple of functional approaches for efficiently iterating over your data.



        If sublists are evenly sized, and you know the index from where to begin extracting elements, use chain + islice:



        from itertools import chain, islice
        n = 3 # Sublist size.
        i,j = 0,2
        newlist = list(islice(chain.from_iterable(m), i*n + j, None))



        If you don't know the size of your sublists in advance, you can use next to discard the first portion of your data.



        V = chain.from_iterable(m)
        next(v for v in V if v == m[i][j])
        newlist = list(V)
        newlist.insert(m[i][j], 0)


        This assumes there is no identical value earlier in the sequence.






        share|improve this answer






















        • These solutions are a bit too advanced, I'm expected to use more basic code. Thank you though
          – Cheetaiean
          Nov 11 at 22:51














        up vote
        3
        down vote













        Here's a couple of functional approaches for efficiently iterating over your data.



        If sublists are evenly sized, and you know the index from where to begin extracting elements, use chain + islice:



        from itertools import chain, islice
        n = 3 # Sublist size.
        i,j = 0,2
        newlist = list(islice(chain.from_iterable(m), i*n + j, None))



        If you don't know the size of your sublists in advance, you can use next to discard the first portion of your data.



        V = chain.from_iterable(m)
        next(v for v in V if v == m[i][j])
        newlist = list(V)
        newlist.insert(m[i][j], 0)


        This assumes there is no identical value earlier in the sequence.






        share|improve this answer






















        • These solutions are a bit too advanced, I'm expected to use more basic code. Thank you though
          – Cheetaiean
          Nov 11 at 22:51












        up vote
        3
        down vote










        up vote
        3
        down vote









        Here's a couple of functional approaches for efficiently iterating over your data.



        If sublists are evenly sized, and you know the index from where to begin extracting elements, use chain + islice:



        from itertools import chain, islice
        n = 3 # Sublist size.
        i,j = 0,2
        newlist = list(islice(chain.from_iterable(m), i*n + j, None))



        If you don't know the size of your sublists in advance, you can use next to discard the first portion of your data.



        V = chain.from_iterable(m)
        next(v for v in V if v == m[i][j])
        newlist = list(V)
        newlist.insert(m[i][j], 0)


        This assumes there is no identical value earlier in the sequence.






        share|improve this answer














        Here's a couple of functional approaches for efficiently iterating over your data.



        If sublists are evenly sized, and you know the index from where to begin extracting elements, use chain + islice:



        from itertools import chain, islice
        n = 3 # Sublist size.
        i,j = 0,2
        newlist = list(islice(chain.from_iterable(m), i*n + j, None))



        If you don't know the size of your sublists in advance, you can use next to discard the first portion of your data.



        V = chain.from_iterable(m)
        next(v for v in V if v == m[i][j])
        newlist = list(V)
        newlist.insert(m[i][j], 0)


        This assumes there is no identical value earlier in the sequence.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 23:46

























        answered Nov 11 at 22:36









        coldspeed

        114k18105182




        114k18105182











        • These solutions are a bit too advanced, I'm expected to use more basic code. Thank you though
          – Cheetaiean
          Nov 11 at 22:51
















        • These solutions are a bit too advanced, I'm expected to use more basic code. Thank you though
          – Cheetaiean
          Nov 11 at 22:51















        These solutions are a bit too advanced, I'm expected to use more basic code. Thank you though
        – Cheetaiean
        Nov 11 at 22:51




        These solutions are a bit too advanced, I'm expected to use more basic code. Thank you though
        – Cheetaiean
        Nov 11 at 22:51










        up vote
        2
        down vote













        You can directly slice off the remainder of the first target list and then add on all subsequent elements, eg:



        m = [[1,2,3],[4,5,10],[6,2,1]]
        y, x = 0, 2
        new_list = m[y][x:] + [v for el in m[y+1:] for v in el]
        # [3, 4, 5, 10, 6, 2, 1]





        share|improve this answer
























          up vote
          2
          down vote













          You can directly slice off the remainder of the first target list and then add on all subsequent elements, eg:



          m = [[1,2,3],[4,5,10],[6,2,1]]
          y, x = 0, 2
          new_list = m[y][x:] + [v for el in m[y+1:] for v in el]
          # [3, 4, 5, 10, 6, 2, 1]





          share|improve this answer






















            up vote
            2
            down vote










            up vote
            2
            down vote









            You can directly slice off the remainder of the first target list and then add on all subsequent elements, eg:



            m = [[1,2,3],[4,5,10],[6,2,1]]
            y, x = 0, 2
            new_list = m[y][x:] + [v for el in m[y+1:] for v in el]
            # [3, 4, 5, 10, 6, 2, 1]





            share|improve this answer












            You can directly slice off the remainder of the first target list and then add on all subsequent elements, eg:



            m = [[1,2,3],[4,5,10],[6,2,1]]
            y, x = 0, 2
            new_list = m[y][x:] + [v for el in m[y+1:] for v in el]
            # [3, 4, 5, 10, 6, 2, 1]






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 22:46









            Jon Clements

            97.8k19173217




            97.8k19173217




















                up vote
                1
                down vote













                why not flatten the initial list and go from there



                flat_list = [item for sublist in m for item in sublist]



                would return [1,2,3,4,5,10,6,2,1] so now you're really on flat_list[2:]






                share|improve this answer




















                • It is in a function in which specific_point serves as an input, i.e. def find_value(m, specific_point)
                  – Cheetaiean
                  Nov 11 at 22:36










                • right but once you flatten the list all you have to do is compute the first index of the slice, based on the specific point
                  – vencaslac
                  Nov 11 at 22:41










                • Just pointing out, this will create a new flattened list, and then flat_list[:2] will create yet another list with the sliced elements.
                  – coldspeed
                  Nov 11 at 22:43











                • @vencaslac You're right, but that only works while specific_point[0] is 0. How about if specific point was [2,1] for example.
                  – Cheetaiean
                  Nov 11 at 22:43











                • 2xlen(sublist)+1
                  – vencaslac
                  Nov 11 at 22:44














                up vote
                1
                down vote













                why not flatten the initial list and go from there



                flat_list = [item for sublist in m for item in sublist]



                would return [1,2,3,4,5,10,6,2,1] so now you're really on flat_list[2:]






                share|improve this answer




















                • It is in a function in which specific_point serves as an input, i.e. def find_value(m, specific_point)
                  – Cheetaiean
                  Nov 11 at 22:36










                • right but once you flatten the list all you have to do is compute the first index of the slice, based on the specific point
                  – vencaslac
                  Nov 11 at 22:41










                • Just pointing out, this will create a new flattened list, and then flat_list[:2] will create yet another list with the sliced elements.
                  – coldspeed
                  Nov 11 at 22:43











                • @vencaslac You're right, but that only works while specific_point[0] is 0. How about if specific point was [2,1] for example.
                  – Cheetaiean
                  Nov 11 at 22:43











                • 2xlen(sublist)+1
                  – vencaslac
                  Nov 11 at 22:44












                up vote
                1
                down vote










                up vote
                1
                down vote









                why not flatten the initial list and go from there



                flat_list = [item for sublist in m for item in sublist]



                would return [1,2,3,4,5,10,6,2,1] so now you're really on flat_list[2:]






                share|improve this answer












                why not flatten the initial list and go from there



                flat_list = [item for sublist in m for item in sublist]



                would return [1,2,3,4,5,10,6,2,1] so now you're really on flat_list[2:]







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 22:33









                vencaslac

                1,002217




                1,002217











                • It is in a function in which specific_point serves as an input, i.e. def find_value(m, specific_point)
                  – Cheetaiean
                  Nov 11 at 22:36










                • right but once you flatten the list all you have to do is compute the first index of the slice, based on the specific point
                  – vencaslac
                  Nov 11 at 22:41










                • Just pointing out, this will create a new flattened list, and then flat_list[:2] will create yet another list with the sliced elements.
                  – coldspeed
                  Nov 11 at 22:43











                • @vencaslac You're right, but that only works while specific_point[0] is 0. How about if specific point was [2,1] for example.
                  – Cheetaiean
                  Nov 11 at 22:43











                • 2xlen(sublist)+1
                  – vencaslac
                  Nov 11 at 22:44
















                • It is in a function in which specific_point serves as an input, i.e. def find_value(m, specific_point)
                  – Cheetaiean
                  Nov 11 at 22:36










                • right but once you flatten the list all you have to do is compute the first index of the slice, based on the specific point
                  – vencaslac
                  Nov 11 at 22:41










                • Just pointing out, this will create a new flattened list, and then flat_list[:2] will create yet another list with the sliced elements.
                  – coldspeed
                  Nov 11 at 22:43











                • @vencaslac You're right, but that only works while specific_point[0] is 0. How about if specific point was [2,1] for example.
                  – Cheetaiean
                  Nov 11 at 22:43











                • 2xlen(sublist)+1
                  – vencaslac
                  Nov 11 at 22:44















                It is in a function in which specific_point serves as an input, i.e. def find_value(m, specific_point)
                – Cheetaiean
                Nov 11 at 22:36




                It is in a function in which specific_point serves as an input, i.e. def find_value(m, specific_point)
                – Cheetaiean
                Nov 11 at 22:36












                right but once you flatten the list all you have to do is compute the first index of the slice, based on the specific point
                – vencaslac
                Nov 11 at 22:41




                right but once you flatten the list all you have to do is compute the first index of the slice, based on the specific point
                – vencaslac
                Nov 11 at 22:41












                Just pointing out, this will create a new flattened list, and then flat_list[:2] will create yet another list with the sliced elements.
                – coldspeed
                Nov 11 at 22:43





                Just pointing out, this will create a new flattened list, and then flat_list[:2] will create yet another list with the sliced elements.
                – coldspeed
                Nov 11 at 22:43













                @vencaslac You're right, but that only works while specific_point[0] is 0. How about if specific point was [2,1] for example.
                – Cheetaiean
                Nov 11 at 22:43





                @vencaslac You're right, but that only works while specific_point[0] is 0. How about if specific point was [2,1] for example.
                – Cheetaiean
                Nov 11 at 22:43













                2xlen(sublist)+1
                – vencaslac
                Nov 11 at 22:44




                2xlen(sublist)+1
                – vencaslac
                Nov 11 at 22:44










                up vote
                1
                down vote













                Most of the answers only work for this specific shape of nested list, but it's also possible to create a solution that works with any shape of nested list.



                def flatten_from(sequence, path=):
                start = path.pop(0) if path else 0
                for item in sequence[start:]:
                if isinstance(item, (list, tuple)):
                yield from flatten_from(item, path)
                else:
                yield item


                With the example from the question



                >>> list(flatten_from([[1, 2, 3], [4, 5, 10], [6, 2, 1]], [0, 2]))
                [3, 4, 5, 10, 6, 2, 1]


                It also works with any shape and level of nesting of the input data



                m = [[1], [[2], [3, 4, 5, 6, 7]], 8, [9, [10, 11]]]

                flatten_from(m, )) # 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
                flatten_from(m, [2]) # 8, 9, 10, 11
                flatten_from(m, [1, 1, 3]) # 6, 7, 8, 9, 10, 11


                This is a bit of a bastard algorithm, though. On one hand, it uses nice functional programming concepts: recursion and yield.



                On the other hand it relies on the side effect of mutating the path argument with list.pop, so it's not a pure function.






                share|improve this answer


























                  up vote
                  1
                  down vote













                  Most of the answers only work for this specific shape of nested list, but it's also possible to create a solution that works with any shape of nested list.



                  def flatten_from(sequence, path=):
                  start = path.pop(0) if path else 0
                  for item in sequence[start:]:
                  if isinstance(item, (list, tuple)):
                  yield from flatten_from(item, path)
                  else:
                  yield item


                  With the example from the question



                  >>> list(flatten_from([[1, 2, 3], [4, 5, 10], [6, 2, 1]], [0, 2]))
                  [3, 4, 5, 10, 6, 2, 1]


                  It also works with any shape and level of nesting of the input data



                  m = [[1], [[2], [3, 4, 5, 6, 7]], 8, [9, [10, 11]]]

                  flatten_from(m, )) # 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
                  flatten_from(m, [2]) # 8, 9, 10, 11
                  flatten_from(m, [1, 1, 3]) # 6, 7, 8, 9, 10, 11


                  This is a bit of a bastard algorithm, though. On one hand, it uses nice functional programming concepts: recursion and yield.



                  On the other hand it relies on the side effect of mutating the path argument with list.pop, so it's not a pure function.






                  share|improve this answer
























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    Most of the answers only work for this specific shape of nested list, but it's also possible to create a solution that works with any shape of nested list.



                    def flatten_from(sequence, path=):
                    start = path.pop(0) if path else 0
                    for item in sequence[start:]:
                    if isinstance(item, (list, tuple)):
                    yield from flatten_from(item, path)
                    else:
                    yield item


                    With the example from the question



                    >>> list(flatten_from([[1, 2, 3], [4, 5, 10], [6, 2, 1]], [0, 2]))
                    [3, 4, 5, 10, 6, 2, 1]


                    It also works with any shape and level of nesting of the input data



                    m = [[1], [[2], [3, 4, 5, 6, 7]], 8, [9, [10, 11]]]

                    flatten_from(m, )) # 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
                    flatten_from(m, [2]) # 8, 9, 10, 11
                    flatten_from(m, [1, 1, 3]) # 6, 7, 8, 9, 10, 11


                    This is a bit of a bastard algorithm, though. On one hand, it uses nice functional programming concepts: recursion and yield.



                    On the other hand it relies on the side effect of mutating the path argument with list.pop, so it's not a pure function.






                    share|improve this answer














                    Most of the answers only work for this specific shape of nested list, but it's also possible to create a solution that works with any shape of nested list.



                    def flatten_from(sequence, path=):
                    start = path.pop(0) if path else 0
                    for item in sequence[start:]:
                    if isinstance(item, (list, tuple)):
                    yield from flatten_from(item, path)
                    else:
                    yield item


                    With the example from the question



                    >>> list(flatten_from([[1, 2, 3], [4, 5, 10], [6, 2, 1]], [0, 2]))
                    [3, 4, 5, 10, 6, 2, 1]


                    It also works with any shape and level of nesting of the input data



                    m = [[1], [[2], [3, 4, 5, 6, 7]], 8, [9, [10, 11]]]

                    flatten_from(m, )) # 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
                    flatten_from(m, [2]) # 8, 9, 10, 11
                    flatten_from(m, [1, 1, 3]) # 6, 7, 8, 9, 10, 11


                    This is a bit of a bastard algorithm, though. On one hand, it uses nice functional programming concepts: recursion and yield.



                    On the other hand it relies on the side effect of mutating the path argument with list.pop, so it's not a pure function.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 11 at 23:52

























                    answered Nov 11 at 23:24









                    Håken Lid

                    10.5k62441




                    10.5k62441




















                        up vote
                        0
                        down vote













                        Below solution will work for your case where your array is restricted to list of list and the size of 'sublist' is consistent throughout i.e "3" in your case



                        m = [[1,2,3],[4,5,10],[6,2,1]] #input 2D array
                        a, b = 0, 2 #user input --> specific point a and b
                        flat_list_m = [item for firstlist in m for item in firstlist] #flat the 2D list
                        print (flat_list_m[len(m[0])*a+b:]) #print from specific position a and b, considering your sublist length is consistent throughout.


                        I hope this helps! :)






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          Below solution will work for your case where your array is restricted to list of list and the size of 'sublist' is consistent throughout i.e "3" in your case



                          m = [[1,2,3],[4,5,10],[6,2,1]] #input 2D array
                          a, b = 0, 2 #user input --> specific point a and b
                          flat_list_m = [item for firstlist in m for item in firstlist] #flat the 2D list
                          print (flat_list_m[len(m[0])*a+b:]) #print from specific position a and b, considering your sublist length is consistent throughout.


                          I hope this helps! :)






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Below solution will work for your case where your array is restricted to list of list and the size of 'sublist' is consistent throughout i.e "3" in your case



                            m = [[1,2,3],[4,5,10],[6,2,1]] #input 2D array
                            a, b = 0, 2 #user input --> specific point a and b
                            flat_list_m = [item for firstlist in m for item in firstlist] #flat the 2D list
                            print (flat_list_m[len(m[0])*a+b:]) #print from specific position a and b, considering your sublist length is consistent throughout.


                            I hope this helps! :)






                            share|improve this answer












                            Below solution will work for your case where your array is restricted to list of list and the size of 'sublist' is consistent throughout i.e "3" in your case



                            m = [[1,2,3],[4,5,10],[6,2,1]] #input 2D array
                            a, b = 0, 2 #user input --> specific point a and b
                            flat_list_m = [item for firstlist in m for item in firstlist] #flat the 2D list
                            print (flat_list_m[len(m[0])*a+b:]) #print from specific position a and b, considering your sublist length is consistent throughout.


                            I hope this helps! :)







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 at 6:18









                            Akash Swain

                            813




                            813



























                                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.





                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253871%2floop-from-a-specific-point-in-a-list-of-lists-python%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







                                Popular posts from this blog

                                Top Tejano songwriter Luis Silva dead of heart attack at 64

                                ReactJS Fetched API data displays live - need Data displayed static

                                政党