python - merging lists from 2 text files












2














I'm very new to Python. I want to take the contents of 2 text files, convert them into 2 lists, merge them together and order them from smallest to biggest.



Converting them to lists seems easy, but I can't merge properly for the ordering.



They come out like this (a list of lists instead of a single list):
['[0, 4, 6, 6, 22, 23, 44]', '[1, 4, 5, 6, 7, 22, 777]']



Here's my code:



with open('numbers1.txt','r') as newfile1:
list1 = newfile1.read().splitlines()
print (list1)

with open('numbers2.txt','r') as newfile2:
list2 = newfile2.read().splitlines()
print (list2)

merged =
for i in range(0, len(list1)) :
merged.append(list1[i])
merged.append(list2[i])
print(merged)


Thanks in advance for any assistance.










share|improve this question





























    2














    I'm very new to Python. I want to take the contents of 2 text files, convert them into 2 lists, merge them together and order them from smallest to biggest.



    Converting them to lists seems easy, but I can't merge properly for the ordering.



    They come out like this (a list of lists instead of a single list):
    ['[0, 4, 6, 6, 22, 23, 44]', '[1, 4, 5, 6, 7, 22, 777]']



    Here's my code:



    with open('numbers1.txt','r') as newfile1:
    list1 = newfile1.read().splitlines()
    print (list1)

    with open('numbers2.txt','r') as newfile2:
    list2 = newfile2.read().splitlines()
    print (list2)

    merged =
    for i in range(0, len(list1)) :
    merged.append(list1[i])
    merged.append(list2[i])
    print(merged)


    Thanks in advance for any assistance.










    share|improve this question



























      2












      2








      2







      I'm very new to Python. I want to take the contents of 2 text files, convert them into 2 lists, merge them together and order them from smallest to biggest.



      Converting them to lists seems easy, but I can't merge properly for the ordering.



      They come out like this (a list of lists instead of a single list):
      ['[0, 4, 6, 6, 22, 23, 44]', '[1, 4, 5, 6, 7, 22, 777]']



      Here's my code:



      with open('numbers1.txt','r') as newfile1:
      list1 = newfile1.read().splitlines()
      print (list1)

      with open('numbers2.txt','r') as newfile2:
      list2 = newfile2.read().splitlines()
      print (list2)

      merged =
      for i in range(0, len(list1)) :
      merged.append(list1[i])
      merged.append(list2[i])
      print(merged)


      Thanks in advance for any assistance.










      share|improve this question















      I'm very new to Python. I want to take the contents of 2 text files, convert them into 2 lists, merge them together and order them from smallest to biggest.



      Converting them to lists seems easy, but I can't merge properly for the ordering.



      They come out like this (a list of lists instead of a single list):
      ['[0, 4, 6, 6, 22, 23, 44]', '[1, 4, 5, 6, 7, 22, 777]']



      Here's my code:



      with open('numbers1.txt','r') as newfile1:
      list1 = newfile1.read().splitlines()
      print (list1)

      with open('numbers2.txt','r') as newfile2:
      list2 = newfile2.read().splitlines()
      print (list2)

      merged =
      for i in range(0, len(list1)) :
      merged.append(list1[i])
      merged.append(list2[i])
      print(merged)


      Thanks in advance for any assistance.







      python python-3.x list






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 14:48









      Raoslaw Szamszur

      895415




      895415










      asked Nov 19 '18 at 12:46









      Murray

      143




      143
























          1 Answer
          1






          active

          oldest

          votes


















          2














          First of all, to join to lists you can use simple + operator:



          >>> l1 = [1, 2, 3]
          >>> l2 = [4, 5, 6]
          >>> merged = l1 + l2
          >>> print(merged)
          [1, 2, 3, 4, 5, 6]


          Now for sorting use python built-in sort() function:




          Python lists have a built-in list.sort() method that modifies the list
          in-place.
          By default, sort() doesn't require any extra parameters. However, it
          has two optional parameters:



          reverse - If true, the sorted list is reversed (or sorted in
          Descending order)



          key - function that serves as a key for the sort
          comparison




          >>> l1 = [1, 2, 3]
          >>> l2 = [4, 5, 6]
          >>> merged = l2 + l1
          >>> print(merged)
          [4, 5, 6, 1, 2, 3]
          >>> merged.sort(key=int)
          >>> print(merged)
          [1, 2, 3, 4, 5, 6]


          Edit



          @Murray above-mentioned solution works just fine but it doesn't fit your use case since in your text files you have list alike string:




          This is numbers1.txt: [0, 4, 6, 6, 22, 23, 44] and this is numbers2.txt: [1, 4, 5, 6, 7, 22, 777] – Murray 1 hour ago




          Now when you read file as list1/list2 actually you'll get a list with one element which is a string.



          >>> with open("numbers1.txt", "r") as f:
          ... print(list1)
          ... print(type(list1))
          ... print(len(list1))
          ... print(type(list1[0]))
          ...
          ['[0, 4, 6, 6, 22, 23, 44]']
          <type 'list'>
          1
          <type 'str'>


          In order to append those numbers from file you'll need to parse them first. It can be achieved like so (depending on your exact use case final solution may vary):



          $ cat numbers1.txt 
          [0, 4, 6, 6, 22, 23, 44]
          $ cat numbers2.txt
          [1, 4, 5, 6, 7, 22, 777]
          $ cat test.py
          files = ['numbers1.txt', 'numbers2.txt']
          merged =

          for f in files:
          with open(f,'r') as lines:
          for line in lines:
          for num in line.rstrip().strip('').split(', '):
          merged.append(int(num))

          print(merged)
          merged.sort()
          print(merged)
          $ python test.py
          [0, 4, 6, 6, 22, 23, 44, 1, 4, 5, 6, 7, 22, 777]
          [0, 1, 4, 4, 5, 6, 6, 6, 7, 22, 22, 23, 44, 777]
          $ python3 test.py
          [0, 4, 6, 6, 22, 23, 44, 1, 4, 5, 6, 7, 22, 777]
          [0, 1, 4, 4, 5, 6, 6, 6, 7, 22, 22, 23, 44, 777]


          Now let me break down this code into pieces:




          1. First of all open file(s). I have done it with for loop so I don't have to repeat with open... every time I want to open something (It's just easier and the code is more readable).

          2. Read all lines from a file. (I've assumed that your file can have more than just one line with list like string)

          3. Parse each line and append to list.



            • line.rstrip() - remove a trailing newline


            • .strip('') - remove square brackets from string


            • .split(', ') - split string by comma and space to get array of string characters


            • merged.append(int(num)) for each character parse it to int and append to list.



          4. Last but not least sort list.






          share|improve this answer























          • Thanks for your response, but I get the same result when I just add the lists together. I have seen many posts on lists, but none that can fully solve my little issue.
            – Murray
            Nov 19 '18 at 19:36










          • @Murray Can you provide an example of what is in those files? I bet list1 = newfile1.read().splitlines() returns you nested list. That's why when you'll later add them it will be a list in list. At least that is my guess.
            – Raoslaw Szamszur
            Nov 19 '18 at 20:31










          • sounds like that could be the problem: This is numbers1.txt: [0, 4, 6, 6, 22, 23, 44] and this is numbers2.txt: [1, 4, 5, 6, 7, 22, 777]
            – Murray
            Nov 20 '18 at 14:32












          • @Murray For the future it's better to add that kind of edit into question rather than in a comment. Anyways, just to clarify your text files contains list like string right?
            – Raoslaw Szamszur
            Nov 20 '18 at 14:58










          • @Murray Does your use case require to have such format in txt file? Reason I'm asking is because if you have influecnce on how numbers are being stored in txt file it can be simplified. If not then of course it can be parsed with list like string but the code will be a bit more complex.
            – Raoslaw Szamszur
            Nov 20 '18 at 15:12











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374965%2fpython-merging-lists-from-2-text-files%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          First of all, to join to lists you can use simple + operator:



          >>> l1 = [1, 2, 3]
          >>> l2 = [4, 5, 6]
          >>> merged = l1 + l2
          >>> print(merged)
          [1, 2, 3, 4, 5, 6]


          Now for sorting use python built-in sort() function:




          Python lists have a built-in list.sort() method that modifies the list
          in-place.
          By default, sort() doesn't require any extra parameters. However, it
          has two optional parameters:



          reverse - If true, the sorted list is reversed (or sorted in
          Descending order)



          key - function that serves as a key for the sort
          comparison




          >>> l1 = [1, 2, 3]
          >>> l2 = [4, 5, 6]
          >>> merged = l2 + l1
          >>> print(merged)
          [4, 5, 6, 1, 2, 3]
          >>> merged.sort(key=int)
          >>> print(merged)
          [1, 2, 3, 4, 5, 6]


          Edit



          @Murray above-mentioned solution works just fine but it doesn't fit your use case since in your text files you have list alike string:




          This is numbers1.txt: [0, 4, 6, 6, 22, 23, 44] and this is numbers2.txt: [1, 4, 5, 6, 7, 22, 777] – Murray 1 hour ago




          Now when you read file as list1/list2 actually you'll get a list with one element which is a string.



          >>> with open("numbers1.txt", "r") as f:
          ... print(list1)
          ... print(type(list1))
          ... print(len(list1))
          ... print(type(list1[0]))
          ...
          ['[0, 4, 6, 6, 22, 23, 44]']
          <type 'list'>
          1
          <type 'str'>


          In order to append those numbers from file you'll need to parse them first. It can be achieved like so (depending on your exact use case final solution may vary):



          $ cat numbers1.txt 
          [0, 4, 6, 6, 22, 23, 44]
          $ cat numbers2.txt
          [1, 4, 5, 6, 7, 22, 777]
          $ cat test.py
          files = ['numbers1.txt', 'numbers2.txt']
          merged =

          for f in files:
          with open(f,'r') as lines:
          for line in lines:
          for num in line.rstrip().strip('').split(', '):
          merged.append(int(num))

          print(merged)
          merged.sort()
          print(merged)
          $ python test.py
          [0, 4, 6, 6, 22, 23, 44, 1, 4, 5, 6, 7, 22, 777]
          [0, 1, 4, 4, 5, 6, 6, 6, 7, 22, 22, 23, 44, 777]
          $ python3 test.py
          [0, 4, 6, 6, 22, 23, 44, 1, 4, 5, 6, 7, 22, 777]
          [0, 1, 4, 4, 5, 6, 6, 6, 7, 22, 22, 23, 44, 777]


          Now let me break down this code into pieces:




          1. First of all open file(s). I have done it with for loop so I don't have to repeat with open... every time I want to open something (It's just easier and the code is more readable).

          2. Read all lines from a file. (I've assumed that your file can have more than just one line with list like string)

          3. Parse each line and append to list.



            • line.rstrip() - remove a trailing newline


            • .strip('') - remove square brackets from string


            • .split(', ') - split string by comma and space to get array of string characters


            • merged.append(int(num)) for each character parse it to int and append to list.



          4. Last but not least sort list.






          share|improve this answer























          • Thanks for your response, but I get the same result when I just add the lists together. I have seen many posts on lists, but none that can fully solve my little issue.
            – Murray
            Nov 19 '18 at 19:36










          • @Murray Can you provide an example of what is in those files? I bet list1 = newfile1.read().splitlines() returns you nested list. That's why when you'll later add them it will be a list in list. At least that is my guess.
            – Raoslaw Szamszur
            Nov 19 '18 at 20:31










          • sounds like that could be the problem: This is numbers1.txt: [0, 4, 6, 6, 22, 23, 44] and this is numbers2.txt: [1, 4, 5, 6, 7, 22, 777]
            – Murray
            Nov 20 '18 at 14:32












          • @Murray For the future it's better to add that kind of edit into question rather than in a comment. Anyways, just to clarify your text files contains list like string right?
            – Raoslaw Szamszur
            Nov 20 '18 at 14:58










          • @Murray Does your use case require to have such format in txt file? Reason I'm asking is because if you have influecnce on how numbers are being stored in txt file it can be simplified. If not then of course it can be parsed with list like string but the code will be a bit more complex.
            – Raoslaw Szamszur
            Nov 20 '18 at 15:12
















          2














          First of all, to join to lists you can use simple + operator:



          >>> l1 = [1, 2, 3]
          >>> l2 = [4, 5, 6]
          >>> merged = l1 + l2
          >>> print(merged)
          [1, 2, 3, 4, 5, 6]


          Now for sorting use python built-in sort() function:




          Python lists have a built-in list.sort() method that modifies the list
          in-place.
          By default, sort() doesn't require any extra parameters. However, it
          has two optional parameters:



          reverse - If true, the sorted list is reversed (or sorted in
          Descending order)



          key - function that serves as a key for the sort
          comparison




          >>> l1 = [1, 2, 3]
          >>> l2 = [4, 5, 6]
          >>> merged = l2 + l1
          >>> print(merged)
          [4, 5, 6, 1, 2, 3]
          >>> merged.sort(key=int)
          >>> print(merged)
          [1, 2, 3, 4, 5, 6]


          Edit



          @Murray above-mentioned solution works just fine but it doesn't fit your use case since in your text files you have list alike string:




          This is numbers1.txt: [0, 4, 6, 6, 22, 23, 44] and this is numbers2.txt: [1, 4, 5, 6, 7, 22, 777] – Murray 1 hour ago




          Now when you read file as list1/list2 actually you'll get a list with one element which is a string.



          >>> with open("numbers1.txt", "r") as f:
          ... print(list1)
          ... print(type(list1))
          ... print(len(list1))
          ... print(type(list1[0]))
          ...
          ['[0, 4, 6, 6, 22, 23, 44]']
          <type 'list'>
          1
          <type 'str'>


          In order to append those numbers from file you'll need to parse them first. It can be achieved like so (depending on your exact use case final solution may vary):



          $ cat numbers1.txt 
          [0, 4, 6, 6, 22, 23, 44]
          $ cat numbers2.txt
          [1, 4, 5, 6, 7, 22, 777]
          $ cat test.py
          files = ['numbers1.txt', 'numbers2.txt']
          merged =

          for f in files:
          with open(f,'r') as lines:
          for line in lines:
          for num in line.rstrip().strip('').split(', '):
          merged.append(int(num))

          print(merged)
          merged.sort()
          print(merged)
          $ python test.py
          [0, 4, 6, 6, 22, 23, 44, 1, 4, 5, 6, 7, 22, 777]
          [0, 1, 4, 4, 5, 6, 6, 6, 7, 22, 22, 23, 44, 777]
          $ python3 test.py
          [0, 4, 6, 6, 22, 23, 44, 1, 4, 5, 6, 7, 22, 777]
          [0, 1, 4, 4, 5, 6, 6, 6, 7, 22, 22, 23, 44, 777]


          Now let me break down this code into pieces:




          1. First of all open file(s). I have done it with for loop so I don't have to repeat with open... every time I want to open something (It's just easier and the code is more readable).

          2. Read all lines from a file. (I've assumed that your file can have more than just one line with list like string)

          3. Parse each line and append to list.



            • line.rstrip() - remove a trailing newline


            • .strip('') - remove square brackets from string


            • .split(', ') - split string by comma and space to get array of string characters


            • merged.append(int(num)) for each character parse it to int and append to list.



          4. Last but not least sort list.






          share|improve this answer























          • Thanks for your response, but I get the same result when I just add the lists together. I have seen many posts on lists, but none that can fully solve my little issue.
            – Murray
            Nov 19 '18 at 19:36










          • @Murray Can you provide an example of what is in those files? I bet list1 = newfile1.read().splitlines() returns you nested list. That's why when you'll later add them it will be a list in list. At least that is my guess.
            – Raoslaw Szamszur
            Nov 19 '18 at 20:31










          • sounds like that could be the problem: This is numbers1.txt: [0, 4, 6, 6, 22, 23, 44] and this is numbers2.txt: [1, 4, 5, 6, 7, 22, 777]
            – Murray
            Nov 20 '18 at 14:32












          • @Murray For the future it's better to add that kind of edit into question rather than in a comment. Anyways, just to clarify your text files contains list like string right?
            – Raoslaw Szamszur
            Nov 20 '18 at 14:58










          • @Murray Does your use case require to have such format in txt file? Reason I'm asking is because if you have influecnce on how numbers are being stored in txt file it can be simplified. If not then of course it can be parsed with list like string but the code will be a bit more complex.
            – Raoslaw Szamszur
            Nov 20 '18 at 15:12














          2












          2








          2






          First of all, to join to lists you can use simple + operator:



          >>> l1 = [1, 2, 3]
          >>> l2 = [4, 5, 6]
          >>> merged = l1 + l2
          >>> print(merged)
          [1, 2, 3, 4, 5, 6]


          Now for sorting use python built-in sort() function:




          Python lists have a built-in list.sort() method that modifies the list
          in-place.
          By default, sort() doesn't require any extra parameters. However, it
          has two optional parameters:



          reverse - If true, the sorted list is reversed (or sorted in
          Descending order)



          key - function that serves as a key for the sort
          comparison




          >>> l1 = [1, 2, 3]
          >>> l2 = [4, 5, 6]
          >>> merged = l2 + l1
          >>> print(merged)
          [4, 5, 6, 1, 2, 3]
          >>> merged.sort(key=int)
          >>> print(merged)
          [1, 2, 3, 4, 5, 6]


          Edit



          @Murray above-mentioned solution works just fine but it doesn't fit your use case since in your text files you have list alike string:




          This is numbers1.txt: [0, 4, 6, 6, 22, 23, 44] and this is numbers2.txt: [1, 4, 5, 6, 7, 22, 777] – Murray 1 hour ago




          Now when you read file as list1/list2 actually you'll get a list with one element which is a string.



          >>> with open("numbers1.txt", "r") as f:
          ... print(list1)
          ... print(type(list1))
          ... print(len(list1))
          ... print(type(list1[0]))
          ...
          ['[0, 4, 6, 6, 22, 23, 44]']
          <type 'list'>
          1
          <type 'str'>


          In order to append those numbers from file you'll need to parse them first. It can be achieved like so (depending on your exact use case final solution may vary):



          $ cat numbers1.txt 
          [0, 4, 6, 6, 22, 23, 44]
          $ cat numbers2.txt
          [1, 4, 5, 6, 7, 22, 777]
          $ cat test.py
          files = ['numbers1.txt', 'numbers2.txt']
          merged =

          for f in files:
          with open(f,'r') as lines:
          for line in lines:
          for num in line.rstrip().strip('').split(', '):
          merged.append(int(num))

          print(merged)
          merged.sort()
          print(merged)
          $ python test.py
          [0, 4, 6, 6, 22, 23, 44, 1, 4, 5, 6, 7, 22, 777]
          [0, 1, 4, 4, 5, 6, 6, 6, 7, 22, 22, 23, 44, 777]
          $ python3 test.py
          [0, 4, 6, 6, 22, 23, 44, 1, 4, 5, 6, 7, 22, 777]
          [0, 1, 4, 4, 5, 6, 6, 6, 7, 22, 22, 23, 44, 777]


          Now let me break down this code into pieces:




          1. First of all open file(s). I have done it with for loop so I don't have to repeat with open... every time I want to open something (It's just easier and the code is more readable).

          2. Read all lines from a file. (I've assumed that your file can have more than just one line with list like string)

          3. Parse each line and append to list.



            • line.rstrip() - remove a trailing newline


            • .strip('') - remove square brackets from string


            • .split(', ') - split string by comma and space to get array of string characters


            • merged.append(int(num)) for each character parse it to int and append to list.



          4. Last but not least sort list.






          share|improve this answer














          First of all, to join to lists you can use simple + operator:



          >>> l1 = [1, 2, 3]
          >>> l2 = [4, 5, 6]
          >>> merged = l1 + l2
          >>> print(merged)
          [1, 2, 3, 4, 5, 6]


          Now for sorting use python built-in sort() function:




          Python lists have a built-in list.sort() method that modifies the list
          in-place.
          By default, sort() doesn't require any extra parameters. However, it
          has two optional parameters:



          reverse - If true, the sorted list is reversed (or sorted in
          Descending order)



          key - function that serves as a key for the sort
          comparison




          >>> l1 = [1, 2, 3]
          >>> l2 = [4, 5, 6]
          >>> merged = l2 + l1
          >>> print(merged)
          [4, 5, 6, 1, 2, 3]
          >>> merged.sort(key=int)
          >>> print(merged)
          [1, 2, 3, 4, 5, 6]


          Edit



          @Murray above-mentioned solution works just fine but it doesn't fit your use case since in your text files you have list alike string:




          This is numbers1.txt: [0, 4, 6, 6, 22, 23, 44] and this is numbers2.txt: [1, 4, 5, 6, 7, 22, 777] – Murray 1 hour ago




          Now when you read file as list1/list2 actually you'll get a list with one element which is a string.



          >>> with open("numbers1.txt", "r") as f:
          ... print(list1)
          ... print(type(list1))
          ... print(len(list1))
          ... print(type(list1[0]))
          ...
          ['[0, 4, 6, 6, 22, 23, 44]']
          <type 'list'>
          1
          <type 'str'>


          In order to append those numbers from file you'll need to parse them first. It can be achieved like so (depending on your exact use case final solution may vary):



          $ cat numbers1.txt 
          [0, 4, 6, 6, 22, 23, 44]
          $ cat numbers2.txt
          [1, 4, 5, 6, 7, 22, 777]
          $ cat test.py
          files = ['numbers1.txt', 'numbers2.txt']
          merged =

          for f in files:
          with open(f,'r') as lines:
          for line in lines:
          for num in line.rstrip().strip('').split(', '):
          merged.append(int(num))

          print(merged)
          merged.sort()
          print(merged)
          $ python test.py
          [0, 4, 6, 6, 22, 23, 44, 1, 4, 5, 6, 7, 22, 777]
          [0, 1, 4, 4, 5, 6, 6, 6, 7, 22, 22, 23, 44, 777]
          $ python3 test.py
          [0, 4, 6, 6, 22, 23, 44, 1, 4, 5, 6, 7, 22, 777]
          [0, 1, 4, 4, 5, 6, 6, 6, 7, 22, 22, 23, 44, 777]


          Now let me break down this code into pieces:




          1. First of all open file(s). I have done it with for loop so I don't have to repeat with open... every time I want to open something (It's just easier and the code is more readable).

          2. Read all lines from a file. (I've assumed that your file can have more than just one line with list like string)

          3. Parse each line and append to list.



            • line.rstrip() - remove a trailing newline


            • .strip('') - remove square brackets from string


            • .split(', ') - split string by comma and space to get array of string characters


            • merged.append(int(num)) for each character parse it to int and append to list.



          4. Last but not least sort list.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 16:03

























          answered Nov 19 '18 at 12:55









          Raoslaw Szamszur

          895415




          895415












          • Thanks for your response, but I get the same result when I just add the lists together. I have seen many posts on lists, but none that can fully solve my little issue.
            – Murray
            Nov 19 '18 at 19:36










          • @Murray Can you provide an example of what is in those files? I bet list1 = newfile1.read().splitlines() returns you nested list. That's why when you'll later add them it will be a list in list. At least that is my guess.
            – Raoslaw Szamszur
            Nov 19 '18 at 20:31










          • sounds like that could be the problem: This is numbers1.txt: [0, 4, 6, 6, 22, 23, 44] and this is numbers2.txt: [1, 4, 5, 6, 7, 22, 777]
            – Murray
            Nov 20 '18 at 14:32












          • @Murray For the future it's better to add that kind of edit into question rather than in a comment. Anyways, just to clarify your text files contains list like string right?
            – Raoslaw Szamszur
            Nov 20 '18 at 14:58










          • @Murray Does your use case require to have such format in txt file? Reason I'm asking is because if you have influecnce on how numbers are being stored in txt file it can be simplified. If not then of course it can be parsed with list like string but the code will be a bit more complex.
            – Raoslaw Szamszur
            Nov 20 '18 at 15:12


















          • Thanks for your response, but I get the same result when I just add the lists together. I have seen many posts on lists, but none that can fully solve my little issue.
            – Murray
            Nov 19 '18 at 19:36










          • @Murray Can you provide an example of what is in those files? I bet list1 = newfile1.read().splitlines() returns you nested list. That's why when you'll later add them it will be a list in list. At least that is my guess.
            – Raoslaw Szamszur
            Nov 19 '18 at 20:31










          • sounds like that could be the problem: This is numbers1.txt: [0, 4, 6, 6, 22, 23, 44] and this is numbers2.txt: [1, 4, 5, 6, 7, 22, 777]
            – Murray
            Nov 20 '18 at 14:32












          • @Murray For the future it's better to add that kind of edit into question rather than in a comment. Anyways, just to clarify your text files contains list like string right?
            – Raoslaw Szamszur
            Nov 20 '18 at 14:58










          • @Murray Does your use case require to have such format in txt file? Reason I'm asking is because if you have influecnce on how numbers are being stored in txt file it can be simplified. If not then of course it can be parsed with list like string but the code will be a bit more complex.
            – Raoslaw Szamszur
            Nov 20 '18 at 15:12
















          Thanks for your response, but I get the same result when I just add the lists together. I have seen many posts on lists, but none that can fully solve my little issue.
          – Murray
          Nov 19 '18 at 19:36




          Thanks for your response, but I get the same result when I just add the lists together. I have seen many posts on lists, but none that can fully solve my little issue.
          – Murray
          Nov 19 '18 at 19:36












          @Murray Can you provide an example of what is in those files? I bet list1 = newfile1.read().splitlines() returns you nested list. That's why when you'll later add them it will be a list in list. At least that is my guess.
          – Raoslaw Szamszur
          Nov 19 '18 at 20:31




          @Murray Can you provide an example of what is in those files? I bet list1 = newfile1.read().splitlines() returns you nested list. That's why when you'll later add them it will be a list in list. At least that is my guess.
          – Raoslaw Szamszur
          Nov 19 '18 at 20:31












          sounds like that could be the problem: This is numbers1.txt: [0, 4, 6, 6, 22, 23, 44] and this is numbers2.txt: [1, 4, 5, 6, 7, 22, 777]
          – Murray
          Nov 20 '18 at 14:32






          sounds like that could be the problem: This is numbers1.txt: [0, 4, 6, 6, 22, 23, 44] and this is numbers2.txt: [1, 4, 5, 6, 7, 22, 777]
          – Murray
          Nov 20 '18 at 14:32














          @Murray For the future it's better to add that kind of edit into question rather than in a comment. Anyways, just to clarify your text files contains list like string right?
          – Raoslaw Szamszur
          Nov 20 '18 at 14:58




          @Murray For the future it's better to add that kind of edit into question rather than in a comment. Anyways, just to clarify your text files contains list like string right?
          – Raoslaw Szamszur
          Nov 20 '18 at 14:58












          @Murray Does your use case require to have such format in txt file? Reason I'm asking is because if you have influecnce on how numbers are being stored in txt file it can be simplified. If not then of course it can be parsed with list like string but the code will be a bit more complex.
          – Raoslaw Szamszur
          Nov 20 '18 at 15:12




          @Murray Does your use case require to have such format in txt file? Reason I'm asking is because if you have influecnce on how numbers are being stored in txt file it can be simplified. If not then of course it can be parsed with list like string but the code will be a bit more complex.
          – Raoslaw Szamszur
          Nov 20 '18 at 15:12


















          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%2f53374965%2fpython-merging-lists-from-2-text-files%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

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith