Python extend method for lists decompose elements of the first list in to the second












1















Apparently, I have a problem with my code. I run the below module with Python 3.6.6 Shell. I want to check whether elements of size 4 exists on list a_list and copy them to b_list. There is only one such element ('3m7n'), but it is decomposed in 4 others ('3', 'm', '7', 'n'). Why?



a_list = ['2', '3m7n', '3', '17', None]  
b_list = ['bat', 'zoo', 'next', 'pina', 'pinta']
for i in a_list:
if i==None:
pass
elif len(i)==4:
b_list.extend(i)
b_list
['bat', 'zoo', 'next', 'pina', 'pinta', '3', 'm', '7', 'n']


enter image description here










share|improve this question

























  • Can you please paste the code as text in your question? It is hard people to help with an image of your code.

    – slider
    Nov 26 '18 at 4:00











  • Use append instead of extend. stackoverflow.com/questions/252703/…

    – slider
    Nov 26 '18 at 4:04
















1















Apparently, I have a problem with my code. I run the below module with Python 3.6.6 Shell. I want to check whether elements of size 4 exists on list a_list and copy them to b_list. There is only one such element ('3m7n'), but it is decomposed in 4 others ('3', 'm', '7', 'n'). Why?



a_list = ['2', '3m7n', '3', '17', None]  
b_list = ['bat', 'zoo', 'next', 'pina', 'pinta']
for i in a_list:
if i==None:
pass
elif len(i)==4:
b_list.extend(i)
b_list
['bat', 'zoo', 'next', 'pina', 'pinta', '3', 'm', '7', 'n']


enter image description here










share|improve this question

























  • Can you please paste the code as text in your question? It is hard people to help with an image of your code.

    – slider
    Nov 26 '18 at 4:00











  • Use append instead of extend. stackoverflow.com/questions/252703/…

    – slider
    Nov 26 '18 at 4:04














1












1








1








Apparently, I have a problem with my code. I run the below module with Python 3.6.6 Shell. I want to check whether elements of size 4 exists on list a_list and copy them to b_list. There is only one such element ('3m7n'), but it is decomposed in 4 others ('3', 'm', '7', 'n'). Why?



a_list = ['2', '3m7n', '3', '17', None]  
b_list = ['bat', 'zoo', 'next', 'pina', 'pinta']
for i in a_list:
if i==None:
pass
elif len(i)==4:
b_list.extend(i)
b_list
['bat', 'zoo', 'next', 'pina', 'pinta', '3', 'm', '7', 'n']


enter image description here










share|improve this question
















Apparently, I have a problem with my code. I run the below module with Python 3.6.6 Shell. I want to check whether elements of size 4 exists on list a_list and copy them to b_list. There is only one such element ('3m7n'), but it is decomposed in 4 others ('3', 'm', '7', 'n'). Why?



a_list = ['2', '3m7n', '3', '17', None]  
b_list = ['bat', 'zoo', 'next', 'pina', 'pinta']
for i in a_list:
if i==None:
pass
elif len(i)==4:
b_list.extend(i)
b_list
['bat', 'zoo', 'next', 'pina', 'pinta', '3', 'm', '7', 'n']


enter image description here







python loops






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 16:23







Ramon

















asked Nov 26 '18 at 3:57









RamonRamon

156




156













  • Can you please paste the code as text in your question? It is hard people to help with an image of your code.

    – slider
    Nov 26 '18 at 4:00











  • Use append instead of extend. stackoverflow.com/questions/252703/…

    – slider
    Nov 26 '18 at 4:04



















  • Can you please paste the code as text in your question? It is hard people to help with an image of your code.

    – slider
    Nov 26 '18 at 4:00











  • Use append instead of extend. stackoverflow.com/questions/252703/…

    – slider
    Nov 26 '18 at 4:04

















Can you please paste the code as text in your question? It is hard people to help with an image of your code.

– slider
Nov 26 '18 at 4:00





Can you please paste the code as text in your question? It is hard people to help with an image of your code.

– slider
Nov 26 '18 at 4:00













Use append instead of extend. stackoverflow.com/questions/252703/…

– slider
Nov 26 '18 at 4:04





Use append instead of extend. stackoverflow.com/questions/252703/…

– slider
Nov 26 '18 at 4:04












2 Answers
2






active

oldest

votes


















2














extend expects a list
append expects an element



a_list = ['2', '3m7n', '3', '17']
b_list = ['bat', 'zoo', 'next']

b_list.extend([i for i in a_list if len(i) == 4 ])
print(b_list)





share|improve this answer































    0














    append adds an object to the end of the list, as a list is a mutable data type, you can append to the end of a list. extend adds each element in an iterable to the end of the list.



    The mistake you are making is extending a list with a str sequence, if you want to add an object use append:



    a = ['2', '3m7n', '3', '17']
    b = ['bat', 'zoo', 'next']

    for i in a:
    if len(i) == 4:
    b.append(i)

    b
    ['bat', 'zoo', 'next', '3', 'm', '7', 'n']


    The error you made using extend is where you extended the list with a string, which then treats it as an iterable and goes through each character of the string, similar to something like for i in '3m7n': print(i). Using the same context, but converting each item in the loop to a list, you use extend to add each item in the list:



    a = ['2', '3m7n', '3', '17']
    b = ['bat', 'zoo', 'next']

    for i in a:
    if len(i) == 4:
    b.extend([i])

    b
    >>['bat', 'zoo', 'next', '3m7n']


    For the sake of simplicity, readability and a little more efficiency, you can use a list comprehension to replace the for loop:



    b.extend([i for i in a if len(i) == 4 ])


    And if you observe, it works exactly the same as the for loop, by extending the list with a list object.






    share|improve this answer























      Your Answer






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

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

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

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      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%2f53474612%2fpython-extend-method-for-lists-decompose-elements-of-the-first-list-in-to-the-se%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









      2














      extend expects a list
      append expects an element



      a_list = ['2', '3m7n', '3', '17']
      b_list = ['bat', 'zoo', 'next']

      b_list.extend([i for i in a_list if len(i) == 4 ])
      print(b_list)





      share|improve this answer




























        2














        extend expects a list
        append expects an element



        a_list = ['2', '3m7n', '3', '17']
        b_list = ['bat', 'zoo', 'next']

        b_list.extend([i for i in a_list if len(i) == 4 ])
        print(b_list)





        share|improve this answer


























          2












          2








          2







          extend expects a list
          append expects an element



          a_list = ['2', '3m7n', '3', '17']
          b_list = ['bat', 'zoo', 'next']

          b_list.extend([i for i in a_list if len(i) == 4 ])
          print(b_list)





          share|improve this answer













          extend expects a list
          append expects an element



          a_list = ['2', '3m7n', '3', '17']
          b_list = ['bat', 'zoo', 'next']

          b_list.extend([i for i in a_list if len(i) == 4 ])
          print(b_list)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 4:06









          KrishnaKrishna

          6021515




          6021515

























              0














              append adds an object to the end of the list, as a list is a mutable data type, you can append to the end of a list. extend adds each element in an iterable to the end of the list.



              The mistake you are making is extending a list with a str sequence, if you want to add an object use append:



              a = ['2', '3m7n', '3', '17']
              b = ['bat', 'zoo', 'next']

              for i in a:
              if len(i) == 4:
              b.append(i)

              b
              ['bat', 'zoo', 'next', '3', 'm', '7', 'n']


              The error you made using extend is where you extended the list with a string, which then treats it as an iterable and goes through each character of the string, similar to something like for i in '3m7n': print(i). Using the same context, but converting each item in the loop to a list, you use extend to add each item in the list:



              a = ['2', '3m7n', '3', '17']
              b = ['bat', 'zoo', 'next']

              for i in a:
              if len(i) == 4:
              b.extend([i])

              b
              >>['bat', 'zoo', 'next', '3m7n']


              For the sake of simplicity, readability and a little more efficiency, you can use a list comprehension to replace the for loop:



              b.extend([i for i in a if len(i) == 4 ])


              And if you observe, it works exactly the same as the for loop, by extending the list with a list object.






              share|improve this answer




























                0














                append adds an object to the end of the list, as a list is a mutable data type, you can append to the end of a list. extend adds each element in an iterable to the end of the list.



                The mistake you are making is extending a list with a str sequence, if you want to add an object use append:



                a = ['2', '3m7n', '3', '17']
                b = ['bat', 'zoo', 'next']

                for i in a:
                if len(i) == 4:
                b.append(i)

                b
                ['bat', 'zoo', 'next', '3', 'm', '7', 'n']


                The error you made using extend is where you extended the list with a string, which then treats it as an iterable and goes through each character of the string, similar to something like for i in '3m7n': print(i). Using the same context, but converting each item in the loop to a list, you use extend to add each item in the list:



                a = ['2', '3m7n', '3', '17']
                b = ['bat', 'zoo', 'next']

                for i in a:
                if len(i) == 4:
                b.extend([i])

                b
                >>['bat', 'zoo', 'next', '3m7n']


                For the sake of simplicity, readability and a little more efficiency, you can use a list comprehension to replace the for loop:



                b.extend([i for i in a if len(i) == 4 ])


                And if you observe, it works exactly the same as the for loop, by extending the list with a list object.






                share|improve this answer


























                  0












                  0








                  0







                  append adds an object to the end of the list, as a list is a mutable data type, you can append to the end of a list. extend adds each element in an iterable to the end of the list.



                  The mistake you are making is extending a list with a str sequence, if you want to add an object use append:



                  a = ['2', '3m7n', '3', '17']
                  b = ['bat', 'zoo', 'next']

                  for i in a:
                  if len(i) == 4:
                  b.append(i)

                  b
                  ['bat', 'zoo', 'next', '3', 'm', '7', 'n']


                  The error you made using extend is where you extended the list with a string, which then treats it as an iterable and goes through each character of the string, similar to something like for i in '3m7n': print(i). Using the same context, but converting each item in the loop to a list, you use extend to add each item in the list:



                  a = ['2', '3m7n', '3', '17']
                  b = ['bat', 'zoo', 'next']

                  for i in a:
                  if len(i) == 4:
                  b.extend([i])

                  b
                  >>['bat', 'zoo', 'next', '3m7n']


                  For the sake of simplicity, readability and a little more efficiency, you can use a list comprehension to replace the for loop:



                  b.extend([i for i in a if len(i) == 4 ])


                  And if you observe, it works exactly the same as the for loop, by extending the list with a list object.






                  share|improve this answer













                  append adds an object to the end of the list, as a list is a mutable data type, you can append to the end of a list. extend adds each element in an iterable to the end of the list.



                  The mistake you are making is extending a list with a str sequence, if you want to add an object use append:



                  a = ['2', '3m7n', '3', '17']
                  b = ['bat', 'zoo', 'next']

                  for i in a:
                  if len(i) == 4:
                  b.append(i)

                  b
                  ['bat', 'zoo', 'next', '3', 'm', '7', 'n']


                  The error you made using extend is where you extended the list with a string, which then treats it as an iterable and goes through each character of the string, similar to something like for i in '3m7n': print(i). Using the same context, but converting each item in the loop to a list, you use extend to add each item in the list:



                  a = ['2', '3m7n', '3', '17']
                  b = ['bat', 'zoo', 'next']

                  for i in a:
                  if len(i) == 4:
                  b.extend([i])

                  b
                  >>['bat', 'zoo', 'next', '3m7n']


                  For the sake of simplicity, readability and a little more efficiency, you can use a list comprehension to replace the for loop:



                  b.extend([i for i in a if len(i) == 4 ])


                  And if you observe, it works exactly the same as the for loop, by extending the list with a list object.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 5:21









                  BernardLBernardL

                  2,40811130




                  2,40811130






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474612%2fpython-extend-method-for-lists-decompose-elements-of-the-first-list-in-to-the-se%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

                      Create new schema in PostgreSQL using DBeaver

                      Deepest pit of an array with Javascript: test on Codility

                      Costa Masnaga