How to separate a string into 2 list of lists











up vote
1
down vote

favorite












I got this string:



n
n
NtOtHPtMtDtUtItNtOn
EtStAtEtItTtLtNtItNn
NtPtNtNtNtGtAOtDtCn
n
n
PERMANENTE
PETTINE
n
n


actually if you looks at original string ,you cannot see the t and n ,so I just edited to better understanding.



What is I'm trying to do is separate to 2 different list of lists,for example:



lists1 = [[NOHPMDUINO][ESAEITLNIN][NPNNNGAODC]]
lists2 = [[PERMANENTE][PETTINE]]


I tried to use many methods to solve this, but without success.
at first I removed the new lines at the beginning with .strip('n') method, and I tried to use replace , but I don't know how to make it right.



Thank you zsomko and snakecharmerb,
Using the method of zsomko and adding strip() to remove the newline at the beginning , here is the loop that I did to divide to 2 variables:



var = True
for line in t:
if line !=['']:
if var:
group1.append(line)
else:
group2.append(line)
else:
var = False


I hope this will help to someone :) If somebody has better solution ,more efficient ,I would like to hear










share|improve this question




























    up vote
    1
    down vote

    favorite












    I got this string:



    n
    n
    NtOtHPtMtDtUtItNtOn
    EtStAtEtItTtLtNtItNn
    NtPtNtNtNtGtAOtDtCn
    n
    n
    PERMANENTE
    PETTINE
    n
    n


    actually if you looks at original string ,you cannot see the t and n ,so I just edited to better understanding.



    What is I'm trying to do is separate to 2 different list of lists,for example:



    lists1 = [[NOHPMDUINO][ESAEITLNIN][NPNNNGAODC]]
    lists2 = [[PERMANENTE][PETTINE]]


    I tried to use many methods to solve this, but without success.
    at first I removed the new lines at the beginning with .strip('n') method, and I tried to use replace , but I don't know how to make it right.



    Thank you zsomko and snakecharmerb,
    Using the method of zsomko and adding strip() to remove the newline at the beginning , here is the loop that I did to divide to 2 variables:



    var = True
    for line in t:
    if line !=['']:
    if var:
    group1.append(line)
    else:
    group2.append(line)
    else:
    var = False


    I hope this will help to someone :) If somebody has better solution ,more efficient ,I would like to hear










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I got this string:



      n
      n
      NtOtHPtMtDtUtItNtOn
      EtStAtEtItTtLtNtItNn
      NtPtNtNtNtGtAOtDtCn
      n
      n
      PERMANENTE
      PETTINE
      n
      n


      actually if you looks at original string ,you cannot see the t and n ,so I just edited to better understanding.



      What is I'm trying to do is separate to 2 different list of lists,for example:



      lists1 = [[NOHPMDUINO][ESAEITLNIN][NPNNNGAODC]]
      lists2 = [[PERMANENTE][PETTINE]]


      I tried to use many methods to solve this, but without success.
      at first I removed the new lines at the beginning with .strip('n') method, and I tried to use replace , but I don't know how to make it right.



      Thank you zsomko and snakecharmerb,
      Using the method of zsomko and adding strip() to remove the newline at the beginning , here is the loop that I did to divide to 2 variables:



      var = True
      for line in t:
      if line !=['']:
      if var:
      group1.append(line)
      else:
      group2.append(line)
      else:
      var = False


      I hope this will help to someone :) If somebody has better solution ,more efficient ,I would like to hear










      share|improve this question















      I got this string:



      n
      n
      NtOtHPtMtDtUtItNtOn
      EtStAtEtItTtLtNtItNn
      NtPtNtNtNtGtAOtDtCn
      n
      n
      PERMANENTE
      PETTINE
      n
      n


      actually if you looks at original string ,you cannot see the t and n ,so I just edited to better understanding.



      What is I'm trying to do is separate to 2 different list of lists,for example:



      lists1 = [[NOHPMDUINO][ESAEITLNIN][NPNNNGAODC]]
      lists2 = [[PERMANENTE][PETTINE]]


      I tried to use many methods to solve this, but without success.
      at first I removed the new lines at the beginning with .strip('n') method, and I tried to use replace , but I don't know how to make it right.



      Thank you zsomko and snakecharmerb,
      Using the method of zsomko and adding strip() to remove the newline at the beginning , here is the loop that I did to divide to 2 variables:



      var = True
      for line in t:
      if line !=['']:
      if var:
      group1.append(line)
      else:
      group2.append(line)
      else:
      var = False


      I hope this will help to someone :) If somebody has better solution ,more efficient ,I would like to hear







      python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 8:48

























      asked Nov 10 at 14:40









      LordNord

      346




      346
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You can break the string into separate lines using its splitlines method - this will give you a list of lines without their terminating newline ('n') characters.



          Then you can loop over the list and replace the tab characters with empty strings using the str.replace method.



          >>> for line in s.splitlines():
          ... if not line:
          ... # Skip empty lines
          ... continue
          ... cleaned = line.replace('t', '')
          ... print(cleaned)
          ...
          NOHPMDUINO
          ESAEITLNIN
          NPNNNGAODC
          PERMANENTE
          PETTINE


          Grouping the output in lists of lists is a little trickier. The question doesn't mention the criteria for grouping, so let's assume that lines which are not separated by empty lines should be listed together.



          We can use a generator to iterate over the string, group adjacent lines and emit them as lists like this:



          >>> def g(s):
          ... out =
          ... for line in s.splitlines():
          ... if not line:
          ... if out:
          ... yield out
          ... out =
          ... continue
          ... cleaned = line.replace('t', '')
          ... out.append([cleaned])
          ... if out:
          ... yield out
          ...
          >>>


          The generator collects lines in a list (out) which it yields each time it finds a blank line and the list is not empty; if the list is yielded it is replaced with an empty list. After looping over the lines in the string it yields the list again, if it isn't empty, in case the string didn't end with blank lines.



          Looping over the generator returns the lists of lists in turn.



          >>> for x in g(s):print(x)
          ...
          [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
          [['PERMANENTE'], ['PETTINE']]


          Alternatively, if you want a list of lists of lists, call list on the generator:



          >>> lists = list(g(s))
          >>> print(lists)
          [[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']], [['PERMANENTE'], ['PETTINE']]]


          If you want to assign the result to named variables, you can unpack the call to list:



          >>> group1, group2 = list(g(s))
          >>> group1
          [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
          >>> group2
          [['PERMANENTE'], ['PETTINE']]


          but note to do this you need to know the number of lists that will be generated in advance.






          share|improve this answer























          • Thank you very much @snakecharmerb ,the problem is that i'm trying to define 2 list of lists,that's the hardest point here
            – LordNord
            Nov 11 at 7:43










          • @LordNord I added grouping / listing logic for you.
            – snakecharmerb
            Nov 11 at 9:05










          • wow that's cool, I edited my question ,How I did it, your way looks more faster I think, If I want to divide it by groups like group1 = [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']] and group2 = [['PERMANENTE'], ['PETTINE']] Can I use inside this one iteration using the boolean fuction like I used?
            – LordNord
            Nov 11 at 9:11










          • @LordNord - I've added code that assigns the results to two variables. If you know there will only be two lists then you're method is just as good - the way I have done it is only better if you don't know how many lists are going to be produced.
            – snakecharmerb
            Nov 11 at 9:20


















          up vote
          2
          down vote













          First eliminate the tabs and split the string into lines:



          lines = [line.replace('t', '') for line in string.splitlines()]


          Then the following would yield the list of lists in the variable groups as expected:



          groups = 
          group =
          for line in lines:
          if group and not line:
          groups.append(group)
          group =
          elif line:
          group.append(line)





          share|improve this answer








          New contributor




          zsomko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















            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%2f53240025%2fhow-to-separate-a-string-into-2-list-of-lists%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            You can break the string into separate lines using its splitlines method - this will give you a list of lines without their terminating newline ('n') characters.



            Then you can loop over the list and replace the tab characters with empty strings using the str.replace method.



            >>> for line in s.splitlines():
            ... if not line:
            ... # Skip empty lines
            ... continue
            ... cleaned = line.replace('t', '')
            ... print(cleaned)
            ...
            NOHPMDUINO
            ESAEITLNIN
            NPNNNGAODC
            PERMANENTE
            PETTINE


            Grouping the output in lists of lists is a little trickier. The question doesn't mention the criteria for grouping, so let's assume that lines which are not separated by empty lines should be listed together.



            We can use a generator to iterate over the string, group adjacent lines and emit them as lists like this:



            >>> def g(s):
            ... out =
            ... for line in s.splitlines():
            ... if not line:
            ... if out:
            ... yield out
            ... out =
            ... continue
            ... cleaned = line.replace('t', '')
            ... out.append([cleaned])
            ... if out:
            ... yield out
            ...
            >>>


            The generator collects lines in a list (out) which it yields each time it finds a blank line and the list is not empty; if the list is yielded it is replaced with an empty list. After looping over the lines in the string it yields the list again, if it isn't empty, in case the string didn't end with blank lines.



            Looping over the generator returns the lists of lists in turn.



            >>> for x in g(s):print(x)
            ...
            [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
            [['PERMANENTE'], ['PETTINE']]


            Alternatively, if you want a list of lists of lists, call list on the generator:



            >>> lists = list(g(s))
            >>> print(lists)
            [[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']], [['PERMANENTE'], ['PETTINE']]]


            If you want to assign the result to named variables, you can unpack the call to list:



            >>> group1, group2 = list(g(s))
            >>> group1
            [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
            >>> group2
            [['PERMANENTE'], ['PETTINE']]


            but note to do this you need to know the number of lists that will be generated in advance.






            share|improve this answer























            • Thank you very much @snakecharmerb ,the problem is that i'm trying to define 2 list of lists,that's the hardest point here
              – LordNord
              Nov 11 at 7:43










            • @LordNord I added grouping / listing logic for you.
              – snakecharmerb
              Nov 11 at 9:05










            • wow that's cool, I edited my question ,How I did it, your way looks more faster I think, If I want to divide it by groups like group1 = [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']] and group2 = [['PERMANENTE'], ['PETTINE']] Can I use inside this one iteration using the boolean fuction like I used?
              – LordNord
              Nov 11 at 9:11










            • @LordNord - I've added code that assigns the results to two variables. If you know there will only be two lists then you're method is just as good - the way I have done it is only better if you don't know how many lists are going to be produced.
              – snakecharmerb
              Nov 11 at 9:20















            up vote
            1
            down vote



            accepted










            You can break the string into separate lines using its splitlines method - this will give you a list of lines without their terminating newline ('n') characters.



            Then you can loop over the list and replace the tab characters with empty strings using the str.replace method.



            >>> for line in s.splitlines():
            ... if not line:
            ... # Skip empty lines
            ... continue
            ... cleaned = line.replace('t', '')
            ... print(cleaned)
            ...
            NOHPMDUINO
            ESAEITLNIN
            NPNNNGAODC
            PERMANENTE
            PETTINE


            Grouping the output in lists of lists is a little trickier. The question doesn't mention the criteria for grouping, so let's assume that lines which are not separated by empty lines should be listed together.



            We can use a generator to iterate over the string, group adjacent lines and emit them as lists like this:



            >>> def g(s):
            ... out =
            ... for line in s.splitlines():
            ... if not line:
            ... if out:
            ... yield out
            ... out =
            ... continue
            ... cleaned = line.replace('t', '')
            ... out.append([cleaned])
            ... if out:
            ... yield out
            ...
            >>>


            The generator collects lines in a list (out) which it yields each time it finds a blank line and the list is not empty; if the list is yielded it is replaced with an empty list. After looping over the lines in the string it yields the list again, if it isn't empty, in case the string didn't end with blank lines.



            Looping over the generator returns the lists of lists in turn.



            >>> for x in g(s):print(x)
            ...
            [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
            [['PERMANENTE'], ['PETTINE']]


            Alternatively, if you want a list of lists of lists, call list on the generator:



            >>> lists = list(g(s))
            >>> print(lists)
            [[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']], [['PERMANENTE'], ['PETTINE']]]


            If you want to assign the result to named variables, you can unpack the call to list:



            >>> group1, group2 = list(g(s))
            >>> group1
            [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
            >>> group2
            [['PERMANENTE'], ['PETTINE']]


            but note to do this you need to know the number of lists that will be generated in advance.






            share|improve this answer























            • Thank you very much @snakecharmerb ,the problem is that i'm trying to define 2 list of lists,that's the hardest point here
              – LordNord
              Nov 11 at 7:43










            • @LordNord I added grouping / listing logic for you.
              – snakecharmerb
              Nov 11 at 9:05










            • wow that's cool, I edited my question ,How I did it, your way looks more faster I think, If I want to divide it by groups like group1 = [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']] and group2 = [['PERMANENTE'], ['PETTINE']] Can I use inside this one iteration using the boolean fuction like I used?
              – LordNord
              Nov 11 at 9:11










            • @LordNord - I've added code that assigns the results to two variables. If you know there will only be two lists then you're method is just as good - the way I have done it is only better if you don't know how many lists are going to be produced.
              – snakecharmerb
              Nov 11 at 9:20













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            You can break the string into separate lines using its splitlines method - this will give you a list of lines without their terminating newline ('n') characters.



            Then you can loop over the list and replace the tab characters with empty strings using the str.replace method.



            >>> for line in s.splitlines():
            ... if not line:
            ... # Skip empty lines
            ... continue
            ... cleaned = line.replace('t', '')
            ... print(cleaned)
            ...
            NOHPMDUINO
            ESAEITLNIN
            NPNNNGAODC
            PERMANENTE
            PETTINE


            Grouping the output in lists of lists is a little trickier. The question doesn't mention the criteria for grouping, so let's assume that lines which are not separated by empty lines should be listed together.



            We can use a generator to iterate over the string, group adjacent lines and emit them as lists like this:



            >>> def g(s):
            ... out =
            ... for line in s.splitlines():
            ... if not line:
            ... if out:
            ... yield out
            ... out =
            ... continue
            ... cleaned = line.replace('t', '')
            ... out.append([cleaned])
            ... if out:
            ... yield out
            ...
            >>>


            The generator collects lines in a list (out) which it yields each time it finds a blank line and the list is not empty; if the list is yielded it is replaced with an empty list. After looping over the lines in the string it yields the list again, if it isn't empty, in case the string didn't end with blank lines.



            Looping over the generator returns the lists of lists in turn.



            >>> for x in g(s):print(x)
            ...
            [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
            [['PERMANENTE'], ['PETTINE']]


            Alternatively, if you want a list of lists of lists, call list on the generator:



            >>> lists = list(g(s))
            >>> print(lists)
            [[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']], [['PERMANENTE'], ['PETTINE']]]


            If you want to assign the result to named variables, you can unpack the call to list:



            >>> group1, group2 = list(g(s))
            >>> group1
            [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
            >>> group2
            [['PERMANENTE'], ['PETTINE']]


            but note to do this you need to know the number of lists that will be generated in advance.






            share|improve this answer














            You can break the string into separate lines using its splitlines method - this will give you a list of lines without their terminating newline ('n') characters.



            Then you can loop over the list and replace the tab characters with empty strings using the str.replace method.



            >>> for line in s.splitlines():
            ... if not line:
            ... # Skip empty lines
            ... continue
            ... cleaned = line.replace('t', '')
            ... print(cleaned)
            ...
            NOHPMDUINO
            ESAEITLNIN
            NPNNNGAODC
            PERMANENTE
            PETTINE


            Grouping the output in lists of lists is a little trickier. The question doesn't mention the criteria for grouping, so let's assume that lines which are not separated by empty lines should be listed together.



            We can use a generator to iterate over the string, group adjacent lines and emit them as lists like this:



            >>> def g(s):
            ... out =
            ... for line in s.splitlines():
            ... if not line:
            ... if out:
            ... yield out
            ... out =
            ... continue
            ... cleaned = line.replace('t', '')
            ... out.append([cleaned])
            ... if out:
            ... yield out
            ...
            >>>


            The generator collects lines in a list (out) which it yields each time it finds a blank line and the list is not empty; if the list is yielded it is replaced with an empty list. After looping over the lines in the string it yields the list again, if it isn't empty, in case the string didn't end with blank lines.



            Looping over the generator returns the lists of lists in turn.



            >>> for x in g(s):print(x)
            ...
            [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
            [['PERMANENTE'], ['PETTINE']]


            Alternatively, if you want a list of lists of lists, call list on the generator:



            >>> lists = list(g(s))
            >>> print(lists)
            [[['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']], [['PERMANENTE'], ['PETTINE']]]


            If you want to assign the result to named variables, you can unpack the call to list:



            >>> group1, group2 = list(g(s))
            >>> group1
            [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']]
            >>> group2
            [['PERMANENTE'], ['PETTINE']]


            but note to do this you need to know the number of lists that will be generated in advance.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 9:18

























            answered Nov 10 at 16:02









            snakecharmerb

            8,69132246




            8,69132246












            • Thank you very much @snakecharmerb ,the problem is that i'm trying to define 2 list of lists,that's the hardest point here
              – LordNord
              Nov 11 at 7:43










            • @LordNord I added grouping / listing logic for you.
              – snakecharmerb
              Nov 11 at 9:05










            • wow that's cool, I edited my question ,How I did it, your way looks more faster I think, If I want to divide it by groups like group1 = [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']] and group2 = [['PERMANENTE'], ['PETTINE']] Can I use inside this one iteration using the boolean fuction like I used?
              – LordNord
              Nov 11 at 9:11










            • @LordNord - I've added code that assigns the results to two variables. If you know there will only be two lists then you're method is just as good - the way I have done it is only better if you don't know how many lists are going to be produced.
              – snakecharmerb
              Nov 11 at 9:20


















            • Thank you very much @snakecharmerb ,the problem is that i'm trying to define 2 list of lists,that's the hardest point here
              – LordNord
              Nov 11 at 7:43










            • @LordNord I added grouping / listing logic for you.
              – snakecharmerb
              Nov 11 at 9:05










            • wow that's cool, I edited my question ,How I did it, your way looks more faster I think, If I want to divide it by groups like group1 = [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']] and group2 = [['PERMANENTE'], ['PETTINE']] Can I use inside this one iteration using the boolean fuction like I used?
              – LordNord
              Nov 11 at 9:11










            • @LordNord - I've added code that assigns the results to two variables. If you know there will only be two lists then you're method is just as good - the way I have done it is only better if you don't know how many lists are going to be produced.
              – snakecharmerb
              Nov 11 at 9:20
















            Thank you very much @snakecharmerb ,the problem is that i'm trying to define 2 list of lists,that's the hardest point here
            – LordNord
            Nov 11 at 7:43




            Thank you very much @snakecharmerb ,the problem is that i'm trying to define 2 list of lists,that's the hardest point here
            – LordNord
            Nov 11 at 7:43












            @LordNord I added grouping / listing logic for you.
            – snakecharmerb
            Nov 11 at 9:05




            @LordNord I added grouping / listing logic for you.
            – snakecharmerb
            Nov 11 at 9:05












            wow that's cool, I edited my question ,How I did it, your way looks more faster I think, If I want to divide it by groups like group1 = [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']] and group2 = [['PERMANENTE'], ['PETTINE']] Can I use inside this one iteration using the boolean fuction like I used?
            – LordNord
            Nov 11 at 9:11




            wow that's cool, I edited my question ,How I did it, your way looks more faster I think, If I want to divide it by groups like group1 = [['NOHPMDUINO'], ['ESAEITLNIN'], ['NPNNNGAODC']] and group2 = [['PERMANENTE'], ['PETTINE']] Can I use inside this one iteration using the boolean fuction like I used?
            – LordNord
            Nov 11 at 9:11












            @LordNord - I've added code that assigns the results to two variables. If you know there will only be two lists then you're method is just as good - the way I have done it is only better if you don't know how many lists are going to be produced.
            – snakecharmerb
            Nov 11 at 9:20




            @LordNord - I've added code that assigns the results to two variables. If you know there will only be two lists then you're method is just as good - the way I have done it is only better if you don't know how many lists are going to be produced.
            – snakecharmerb
            Nov 11 at 9:20












            up vote
            2
            down vote













            First eliminate the tabs and split the string into lines:



            lines = [line.replace('t', '') for line in string.splitlines()]


            Then the following would yield the list of lists in the variable groups as expected:



            groups = 
            group =
            for line in lines:
            if group and not line:
            groups.append(group)
            group =
            elif line:
            group.append(line)





            share|improve this answer








            New contributor




            zsomko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






















              up vote
              2
              down vote













              First eliminate the tabs and split the string into lines:



              lines = [line.replace('t', '') for line in string.splitlines()]


              Then the following would yield the list of lists in the variable groups as expected:



              groups = 
              group =
              for line in lines:
              if group and not line:
              groups.append(group)
              group =
              elif line:
              group.append(line)





              share|improve this answer








              New contributor




              zsomko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.




















                up vote
                2
                down vote










                up vote
                2
                down vote









                First eliminate the tabs and split the string into lines:



                lines = [line.replace('t', '') for line in string.splitlines()]


                Then the following would yield the list of lists in the variable groups as expected:



                groups = 
                group =
                for line in lines:
                if group and not line:
                groups.append(group)
                group =
                elif line:
                group.append(line)





                share|improve this answer








                New contributor




                zsomko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                First eliminate the tabs and split the string into lines:



                lines = [line.replace('t', '') for line in string.splitlines()]


                Then the following would yield the list of lists in the variable groups as expected:



                groups = 
                group =
                for line in lines:
                if group and not line:
                groups.append(group)
                group =
                elif line:
                group.append(line)






                share|improve this answer








                New contributor




                zsomko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer






                New contributor




                zsomko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered Nov 10 at 16:01









                zsomko

                212




                212




                New contributor




                zsomko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                zsomko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                zsomko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240025%2fhow-to-separate-a-string-into-2-list-of-lists%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

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues