Manipulating list with sub-lists of different lengths

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite












Consider the following list, which is composed of sublists with different lengths



, 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3


How can this list be transformed into a list of the first elements, second elements, etc...i.e, the out come I would like to produce



2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8









share|improve this question





















  • related: Transpose uneven lists
    – user1066
    Aug 20 at 7:40














up vote
5
down vote

favorite












Consider the following list, which is composed of sublists with different lengths



, 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3


How can this list be transformed into a list of the first elements, second elements, etc...i.e, the out come I would like to produce



2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8









share|improve this question





















  • related: Transpose uneven lists
    – user1066
    Aug 20 at 7:40












up vote
5
down vote

favorite









up vote
5
down vote

favorite











Consider the following list, which is composed of sublists with different lengths



, 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3


How can this list be transformed into a list of the first elements, second elements, etc...i.e, the out come I would like to produce



2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8









share|improve this question













Consider the following list, which is composed of sublists with different lengths



, 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3


How can this list be transformed into a list of the first elements, second elements, etc...i.e, the out come I would like to produce



2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8






list-manipulation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 19 at 14:27









jarhead

689413




689413











  • related: Transpose uneven lists
    – user1066
    Aug 20 at 7:40
















  • related: Transpose uneven lists
    – user1066
    Aug 20 at 7:40















related: Transpose uneven lists
– user1066
Aug 20 at 7:40




related: Transpose uneven lists
– user1066
Aug 20 at 7:40










3 Answers
3






active

oldest

votes

















up vote
4
down vote



accepted










Maybe this way?



data = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3;
get[a_, k_] := Map[If[Length[#] >= k, #[[k]], Nothing] &, a];
get[data, 1]
get[data, 2]
get[data, 3]
get[data, 4]



2, 2, 6, 3, 5, 4, 4, 6, 6, 3



7, 2, 5, 5, 4, 7



4, 8









share|improve this answer



























    up vote
    6
    down vote













    You can use Flatten:



    list = 
    ,
    2,2,7,2,
    6,3,
    5,4,5,5,
    4,6,4,7,4,8,
    ,
    6,3
    ;

    Flatten[list, 2]



    2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8







    share|improve this answer



























      up vote
      4
      down vote













      lst = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5,
      4, 6, 4, 7, 4, 8, , 6, 3;

      Extract[lst, #] & /@ GatherBy[Position[lst, _, 2, Heads -> False], Last]



      2, 2, 6, 3, 5, 4, 4, 6, 6, 3,

      7, 2, 5, 5, 4, 7,

      4, 8




      or



      parts[l_][k_] := Join @@ MapIndexed[If[Last[#2] == k, #, ## &] &, l, 2]
      parts[lst] /@ Range[4]]



      2, 2, 6, 3, 5, 4, 4, 6, 6, 3,

      7, 2, 5, 5, 4, 7,

      4, 8,







      share|improve this answer






















        Your Answer




        StackExchange.ifUsing("editor", function ()
        return StackExchange.using("mathjaxEditing", function ()
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
        StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
        );
        );
        , "mathjax-editing");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "387"
        ;
        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: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180244%2fmanipulating-list-with-sub-lists-of-different-lengths%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        4
        down vote



        accepted










        Maybe this way?



        data = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3;
        get[a_, k_] := Map[If[Length[#] >= k, #[[k]], Nothing] &, a];
        get[data, 1]
        get[data, 2]
        get[data, 3]
        get[data, 4]



        2, 2, 6, 3, 5, 4, 4, 6, 6, 3



        7, 2, 5, 5, 4, 7



        4, 8









        share|improve this answer
























          up vote
          4
          down vote



          accepted










          Maybe this way?



          data = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3;
          get[a_, k_] := Map[If[Length[#] >= k, #[[k]], Nothing] &, a];
          get[data, 1]
          get[data, 2]
          get[data, 3]
          get[data, 4]



          2, 2, 6, 3, 5, 4, 4, 6, 6, 3



          7, 2, 5, 5, 4, 7



          4, 8









          share|improve this answer






















            up vote
            4
            down vote



            accepted







            up vote
            4
            down vote



            accepted






            Maybe this way?



            data = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3;
            get[a_, k_] := Map[If[Length[#] >= k, #[[k]], Nothing] &, a];
            get[data, 1]
            get[data, 2]
            get[data, 3]
            get[data, 4]



            2, 2, 6, 3, 5, 4, 4, 6, 6, 3



            7, 2, 5, 5, 4, 7



            4, 8









            share|improve this answer












            Maybe this way?



            data = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3;
            get[a_, k_] := Map[If[Length[#] >= k, #[[k]], Nothing] &, a];
            get[data, 1]
            get[data, 2]
            get[data, 3]
            get[data, 4]



            2, 2, 6, 3, 5, 4, 4, 6, 6, 3



            7, 2, 5, 5, 4, 7



            4, 8










            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 19 at 14:35









            Henrik Schumacher

            38.7k253114




            38.7k253114




















                up vote
                6
                down vote













                You can use Flatten:



                list = 
                ,
                2,2,7,2,
                6,3,
                5,4,5,5,
                4,6,4,7,4,8,
                ,
                6,3
                ;

                Flatten[list, 2]



                2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8







                share|improve this answer
























                  up vote
                  6
                  down vote













                  You can use Flatten:



                  list = 
                  ,
                  2,2,7,2,
                  6,3,
                  5,4,5,5,
                  4,6,4,7,4,8,
                  ,
                  6,3
                  ;

                  Flatten[list, 2]



                  2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8







                  share|improve this answer






















                    up vote
                    6
                    down vote










                    up vote
                    6
                    down vote









                    You can use Flatten:



                    list = 
                    ,
                    2,2,7,2,
                    6,3,
                    5,4,5,5,
                    4,6,4,7,4,8,
                    ,
                    6,3
                    ;

                    Flatten[list, 2]



                    2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8







                    share|improve this answer












                    You can use Flatten:



                    list = 
                    ,
                    2,2,7,2,
                    6,3,
                    5,4,5,5,
                    4,6,4,7,4,8,
                    ,
                    6,3
                    ;

                    Flatten[list, 2]



                    2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8








                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 19 at 15:00









                    Carl Woll

                    57.7k273149




                    57.7k273149




















                        up vote
                        4
                        down vote













                        lst = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5,
                        4, 6, 4, 7, 4, 8, , 6, 3;

                        Extract[lst, #] & /@ GatherBy[Position[lst, _, 2, Heads -> False], Last]



                        2, 2, 6, 3, 5, 4, 4, 6, 6, 3,

                        7, 2, 5, 5, 4, 7,

                        4, 8




                        or



                        parts[l_][k_] := Join @@ MapIndexed[If[Last[#2] == k, #, ## &] &, l, 2]
                        parts[lst] /@ Range[4]]



                        2, 2, 6, 3, 5, 4, 4, 6, 6, 3,

                        7, 2, 5, 5, 4, 7,

                        4, 8,







                        share|improve this answer


























                          up vote
                          4
                          down vote













                          lst = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5,
                          4, 6, 4, 7, 4, 8, , 6, 3;

                          Extract[lst, #] & /@ GatherBy[Position[lst, _, 2, Heads -> False], Last]



                          2, 2, 6, 3, 5, 4, 4, 6, 6, 3,

                          7, 2, 5, 5, 4, 7,

                          4, 8




                          or



                          parts[l_][k_] := Join @@ MapIndexed[If[Last[#2] == k, #, ## &] &, l, 2]
                          parts[lst] /@ Range[4]]



                          2, 2, 6, 3, 5, 4, 4, 6, 6, 3,

                          7, 2, 5, 5, 4, 7,

                          4, 8,







                          share|improve this answer
























                            up vote
                            4
                            down vote










                            up vote
                            4
                            down vote









                            lst = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5,
                            4, 6, 4, 7, 4, 8, , 6, 3;

                            Extract[lst, #] & /@ GatherBy[Position[lst, _, 2, Heads -> False], Last]



                            2, 2, 6, 3, 5, 4, 4, 6, 6, 3,

                            7, 2, 5, 5, 4, 7,

                            4, 8




                            or



                            parts[l_][k_] := Join @@ MapIndexed[If[Last[#2] == k, #, ## &] &, l, 2]
                            parts[lst] /@ Range[4]]



                            2, 2, 6, 3, 5, 4, 4, 6, 6, 3,

                            7, 2, 5, 5, 4, 7,

                            4, 8,







                            share|improve this answer














                            lst = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5,
                            4, 6, 4, 7, 4, 8, , 6, 3;

                            Extract[lst, #] & /@ GatherBy[Position[lst, _, 2, Heads -> False], Last]



                            2, 2, 6, 3, 5, 4, 4, 6, 6, 3,

                            7, 2, 5, 5, 4, 7,

                            4, 8




                            or



                            parts[l_][k_] := Join @@ MapIndexed[If[Last[#2] == k, #, ## &] &, l, 2]
                            parts[lst] /@ Range[4]]



                            2, 2, 6, 3, 5, 4, 4, 6, 6, 3,

                            7, 2, 5, 5, 4, 7,

                            4, 8,








                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Aug 19 at 15:22

























                            answered Aug 19 at 14:54









                            kglr

                            161k8185384




                            161k8185384



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180244%2fmanipulating-list-with-sub-lists-of-different-lengths%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Displaying single band from multi-band raster using QGIS

                                How many registers does an x86_64 CPU actually have?