Flatten all inner lists that do not have sublists

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











up vote
3
down vote

favorite
1












As the title says, I would like to flatten all inner lists, excluded the ones that do not have sublists. For example, from



lis = a, b, d, f, c, k, h, l, e, a;


I would like to have



myFlatten[lis]

a, b, d, f, c, k, h, l, e, a


I have a solution, but probably is inefficient and not elegant.
The steps are the following:



  • change the Head of all List subparts that do not have members of type List, to something else, say myList


  • Flatten the resulting expression

  • change back myList to List

this is my implementation:



notListQ[z_] := Head[z] =!= List;

myFlatten[list_List] := Module[myList, tempList,
tempList = list /. List[y__?notListQ] -> myList[y];
tempList = Flatten[tempList];
tempList /. myList -> List
]


This obviously can be shortened to



myFlatten[list_List] := Module[myList,
Flatten[list /. List[y__?notListQ] -> myList[y]] /. myList -> List
]


Any errors in my implementation?

Better solutions?



Thank you










share|improve this question

























    up vote
    3
    down vote

    favorite
    1












    As the title says, I would like to flatten all inner lists, excluded the ones that do not have sublists. For example, from



    lis = a, b, d, f, c, k, h, l, e, a;


    I would like to have



    myFlatten[lis]

    a, b, d, f, c, k, h, l, e, a


    I have a solution, but probably is inefficient and not elegant.
    The steps are the following:



    • change the Head of all List subparts that do not have members of type List, to something else, say myList


    • Flatten the resulting expression

    • change back myList to List

    this is my implementation:



    notListQ[z_] := Head[z] =!= List;

    myFlatten[list_List] := Module[myList, tempList,
    tempList = list /. List[y__?notListQ] -> myList[y];
    tempList = Flatten[tempList];
    tempList /. myList -> List
    ]


    This obviously can be shortened to



    myFlatten[list_List] := Module[myList,
    Flatten[list /. List[y__?notListQ] -> myList[y]] /. myList -> List
    ]


    Any errors in my implementation?

    Better solutions?



    Thank you










    share|improve this question























      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      As the title says, I would like to flatten all inner lists, excluded the ones that do not have sublists. For example, from



      lis = a, b, d, f, c, k, h, l, e, a;


      I would like to have



      myFlatten[lis]

      a, b, d, f, c, k, h, l, e, a


      I have a solution, but probably is inefficient and not elegant.
      The steps are the following:



      • change the Head of all List subparts that do not have members of type List, to something else, say myList


      • Flatten the resulting expression

      • change back myList to List

      this is my implementation:



      notListQ[z_] := Head[z] =!= List;

      myFlatten[list_List] := Module[myList, tempList,
      tempList = list /. List[y__?notListQ] -> myList[y];
      tempList = Flatten[tempList];
      tempList /. myList -> List
      ]


      This obviously can be shortened to



      myFlatten[list_List] := Module[myList,
      Flatten[list /. List[y__?notListQ] -> myList[y]] /. myList -> List
      ]


      Any errors in my implementation?

      Better solutions?



      Thank you










      share|improve this question













      As the title says, I would like to flatten all inner lists, excluded the ones that do not have sublists. For example, from



      lis = a, b, d, f, c, k, h, l, e, a;


      I would like to have



      myFlatten[lis]

      a, b, d, f, c, k, h, l, e, a


      I have a solution, but probably is inefficient and not elegant.
      The steps are the following:



      • change the Head of all List subparts that do not have members of type List, to something else, say myList


      • Flatten the resulting expression

      • change back myList to List

      this is my implementation:



      notListQ[z_] := Head[z] =!= List;

      myFlatten[list_List] := Module[myList, tempList,
      tempList = list /. List[y__?notListQ] -> myList[y];
      tempList = Flatten[tempList];
      tempList /. myList -> List
      ]


      This obviously can be shortened to



      myFlatten[list_List] := Module[myList,
      Flatten[list /. List[y__?notListQ] -> myList[y]] /. myList -> List
      ]


      Any errors in my implementation?

      Better solutions?



      Thank you







      list-manipulation






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 38 mins ago









      enzotib

      477415




      477415




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote













          ReleaseHold[Flatten[MapAt[Hold, lis, Position[lis, _List?VectorQ]]]]



          a, b, d, f, c, k, h, l, e, a







          share|improve this answer
















          • 1




            Another variant is ReleaseHold @ Flatten @ Replace[lis, l_List :> Hold[l], -2]
            – Carl Woll
            3 mins ago










          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%2f182646%2fflatten-all-inner-lists-that-do-not-have-sublists%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote













          ReleaseHold[Flatten[MapAt[Hold, lis, Position[lis, _List?VectorQ]]]]



          a, b, d, f, c, k, h, l, e, a







          share|improve this answer
















          • 1




            Another variant is ReleaseHold @ Flatten @ Replace[lis, l_List :> Hold[l], -2]
            – Carl Woll
            3 mins ago














          up vote
          4
          down vote













          ReleaseHold[Flatten[MapAt[Hold, lis, Position[lis, _List?VectorQ]]]]



          a, b, d, f, c, k, h, l, e, a







          share|improve this answer
















          • 1




            Another variant is ReleaseHold @ Flatten @ Replace[lis, l_List :> Hold[l], -2]
            – Carl Woll
            3 mins ago












          up vote
          4
          down vote










          up vote
          4
          down vote









          ReleaseHold[Flatten[MapAt[Hold, lis, Position[lis, _List?VectorQ]]]]



          a, b, d, f, c, k, h, l, e, a







          share|improve this answer












          ReleaseHold[Flatten[MapAt[Hold, lis, Position[lis, _List?VectorQ]]]]



          a, b, d, f, c, k, h, l, e, a








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 31 mins ago









          Coolwater

          13.5k32150




          13.5k32150







          • 1




            Another variant is ReleaseHold @ Flatten @ Replace[lis, l_List :> Hold[l], -2]
            – Carl Woll
            3 mins ago












          • 1




            Another variant is ReleaseHold @ Flatten @ Replace[lis, l_List :> Hold[l], -2]
            – Carl Woll
            3 mins ago







          1




          1




          Another variant is ReleaseHold @ Flatten @ Replace[lis, l_List :> Hold[l], -2]
          – Carl Woll
          3 mins ago




          Another variant is ReleaseHold @ Flatten @ Replace[lis, l_List :> Hold[l], -2]
          – Carl Woll
          3 mins ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182646%2fflatten-all-inner-lists-that-do-not-have-sublists%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

          Confectionery