My code will append a list but not print out the correct length












1















I'm using PyCharm and I have to append and then print out the length of the list. But instead of giving me 4 for all the elements it is giving me 2 for each separate list. I'm not sure what the issue is:



my_list = ['ABC, GHI, JKL']

print('My first list', my_list)

my_list.append('MNO')

print('My appended list', my_list)

print('My list size', len(my_list))









share|improve this question




















  • 3





    mylist is initialized as a list with one string element as 'ABC, GHI, JKL'

    – John Anderson
    Nov 25 '18 at 2:09











  • 'ABC, GHI, JKL' is just one item inside list.

    – Austin
    Nov 25 '18 at 2:09











  • Thank you to everyone that answered!!!

    – Fluffiekitty
    Nov 25 '18 at 18:21
















1















I'm using PyCharm and I have to append and then print out the length of the list. But instead of giving me 4 for all the elements it is giving me 2 for each separate list. I'm not sure what the issue is:



my_list = ['ABC, GHI, JKL']

print('My first list', my_list)

my_list.append('MNO')

print('My appended list', my_list)

print('My list size', len(my_list))









share|improve this question




















  • 3





    mylist is initialized as a list with one string element as 'ABC, GHI, JKL'

    – John Anderson
    Nov 25 '18 at 2:09











  • 'ABC, GHI, JKL' is just one item inside list.

    – Austin
    Nov 25 '18 at 2:09











  • Thank you to everyone that answered!!!

    – Fluffiekitty
    Nov 25 '18 at 18:21














1












1








1








I'm using PyCharm and I have to append and then print out the length of the list. But instead of giving me 4 for all the elements it is giving me 2 for each separate list. I'm not sure what the issue is:



my_list = ['ABC, GHI, JKL']

print('My first list', my_list)

my_list.append('MNO')

print('My appended list', my_list)

print('My list size', len(my_list))









share|improve this question
















I'm using PyCharm and I have to append and then print out the length of the list. But instead of giving me 4 for all the elements it is giving me 2 for each separate list. I'm not sure what the issue is:



my_list = ['ABC, GHI, JKL']

print('My first list', my_list)

my_list.append('MNO')

print('My appended list', my_list)

print('My list size', len(my_list))






python list append string-length






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 2:08









Michael Butscher

4,73521421




4,73521421










asked Nov 25 '18 at 2:07









FluffiekittyFluffiekitty

62




62








  • 3





    mylist is initialized as a list with one string element as 'ABC, GHI, JKL'

    – John Anderson
    Nov 25 '18 at 2:09











  • 'ABC, GHI, JKL' is just one item inside list.

    – Austin
    Nov 25 '18 at 2:09











  • Thank you to everyone that answered!!!

    – Fluffiekitty
    Nov 25 '18 at 18:21














  • 3





    mylist is initialized as a list with one string element as 'ABC, GHI, JKL'

    – John Anderson
    Nov 25 '18 at 2:09











  • 'ABC, GHI, JKL' is just one item inside list.

    – Austin
    Nov 25 '18 at 2:09











  • Thank you to everyone that answered!!!

    – Fluffiekitty
    Nov 25 '18 at 18:21








3




3





mylist is initialized as a list with one string element as 'ABC, GHI, JKL'

– John Anderson
Nov 25 '18 at 2:09





mylist is initialized as a list with one string element as 'ABC, GHI, JKL'

– John Anderson
Nov 25 '18 at 2:09













'ABC, GHI, JKL' is just one item inside list.

– Austin
Nov 25 '18 at 2:09





'ABC, GHI, JKL' is just one item inside list.

– Austin
Nov 25 '18 at 2:09













Thank you to everyone that answered!!!

– Fluffiekitty
Nov 25 '18 at 18:21





Thank you to everyone that answered!!!

– Fluffiekitty
Nov 25 '18 at 18:21












3 Answers
3






active

oldest

votes


















0














my_list is a list of one element, because the container is 'ABC, GHI, JKL' which is one string.



So you need a list with three elements at start as ['ABC', 'GHI', 'JKL'].



Then your code will work as expected.



The output of the code would be:



My first list ['ABC', 'GHI', 'JKL']
My appended list ['ABC', 'GHI', 'JKL', 'MNO']
My list size 4


Which is as desired.






share|improve this answer































    0














    my_list originally has only one element (notice the start and end of quotes). If there is only one pair of quotes and no other elements as integers or floats, there is only one element in list.



    To get a 4 output, you need to split your first element on ', ', like so:



    my_list = ['ABC, GHI, JKL']

    my_list = my_list[0].split(', ') # <---

    print('My first list', my_list)
    my_list.append('MNO')
    print('My appended list', my_list)
    print('My list size', len(my_list))





    share|improve this answer































      0














      Your code absolutly works correct,
      The reason you got 2 as output is that you have given Items as a single string into your list, it means your list has single item of 'ABC, GHI, JKL' and then you append your second list item 'MNO'. Meanwhile should you correct your list at first line, sprating each item using , or using ' within , sperators.



      my_list = ['ABC', 'GHI', 'JKL']
      len(my_list)
      3
      my_list.append('MNO')
      len(my_list)
      4





      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%2f53464076%2fmy-code-will-append-a-list-but-not-print-out-the-correct-length%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        my_list is a list of one element, because the container is 'ABC, GHI, JKL' which is one string.



        So you need a list with three elements at start as ['ABC', 'GHI', 'JKL'].



        Then your code will work as expected.



        The output of the code would be:



        My first list ['ABC', 'GHI', 'JKL']
        My appended list ['ABC', 'GHI', 'JKL', 'MNO']
        My list size 4


        Which is as desired.






        share|improve this answer




























          0














          my_list is a list of one element, because the container is 'ABC, GHI, JKL' which is one string.



          So you need a list with three elements at start as ['ABC', 'GHI', 'JKL'].



          Then your code will work as expected.



          The output of the code would be:



          My first list ['ABC', 'GHI', 'JKL']
          My appended list ['ABC', 'GHI', 'JKL', 'MNO']
          My list size 4


          Which is as desired.






          share|improve this answer


























            0












            0








            0







            my_list is a list of one element, because the container is 'ABC, GHI, JKL' which is one string.



            So you need a list with three elements at start as ['ABC', 'GHI', 'JKL'].



            Then your code will work as expected.



            The output of the code would be:



            My first list ['ABC', 'GHI', 'JKL']
            My appended list ['ABC', 'GHI', 'JKL', 'MNO']
            My list size 4


            Which is as desired.






            share|improve this answer













            my_list is a list of one element, because the container is 'ABC, GHI, JKL' which is one string.



            So you need a list with three elements at start as ['ABC', 'GHI', 'JKL'].



            Then your code will work as expected.



            The output of the code would be:



            My first list ['ABC', 'GHI', 'JKL']
            My appended list ['ABC', 'GHI', 'JKL', 'MNO']
            My list size 4


            Which is as desired.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 25 '18 at 2:12









            U9-ForwardU9-Forward

            15.6k41540




            15.6k41540

























                0














                my_list originally has only one element (notice the start and end of quotes). If there is only one pair of quotes and no other elements as integers or floats, there is only one element in list.



                To get a 4 output, you need to split your first element on ', ', like so:



                my_list = ['ABC, GHI, JKL']

                my_list = my_list[0].split(', ') # <---

                print('My first list', my_list)
                my_list.append('MNO')
                print('My appended list', my_list)
                print('My list size', len(my_list))





                share|improve this answer




























                  0














                  my_list originally has only one element (notice the start and end of quotes). If there is only one pair of quotes and no other elements as integers or floats, there is only one element in list.



                  To get a 4 output, you need to split your first element on ', ', like so:



                  my_list = ['ABC, GHI, JKL']

                  my_list = my_list[0].split(', ') # <---

                  print('My first list', my_list)
                  my_list.append('MNO')
                  print('My appended list', my_list)
                  print('My list size', len(my_list))





                  share|improve this answer


























                    0












                    0








                    0







                    my_list originally has only one element (notice the start and end of quotes). If there is only one pair of quotes and no other elements as integers or floats, there is only one element in list.



                    To get a 4 output, you need to split your first element on ', ', like so:



                    my_list = ['ABC, GHI, JKL']

                    my_list = my_list[0].split(', ') # <---

                    print('My first list', my_list)
                    my_list.append('MNO')
                    print('My appended list', my_list)
                    print('My list size', len(my_list))





                    share|improve this answer













                    my_list originally has only one element (notice the start and end of quotes). If there is only one pair of quotes and no other elements as integers or floats, there is only one element in list.



                    To get a 4 output, you need to split your first element on ', ', like so:



                    my_list = ['ABC, GHI, JKL']

                    my_list = my_list[0].split(', ') # <---

                    print('My first list', my_list)
                    my_list.append('MNO')
                    print('My appended list', my_list)
                    print('My list size', len(my_list))






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 25 '18 at 2:15









                    AustinAustin

                    11k3828




                    11k3828























                        0














                        Your code absolutly works correct,
                        The reason you got 2 as output is that you have given Items as a single string into your list, it means your list has single item of 'ABC, GHI, JKL' and then you append your second list item 'MNO'. Meanwhile should you correct your list at first line, sprating each item using , or using ' within , sperators.



                        my_list = ['ABC', 'GHI', 'JKL']
                        len(my_list)
                        3
                        my_list.append('MNO')
                        len(my_list)
                        4





                        share|improve this answer




























                          0














                          Your code absolutly works correct,
                          The reason you got 2 as output is that you have given Items as a single string into your list, it means your list has single item of 'ABC, GHI, JKL' and then you append your second list item 'MNO'. Meanwhile should you correct your list at first line, sprating each item using , or using ' within , sperators.



                          my_list = ['ABC', 'GHI', 'JKL']
                          len(my_list)
                          3
                          my_list.append('MNO')
                          len(my_list)
                          4





                          share|improve this answer


























                            0












                            0








                            0







                            Your code absolutly works correct,
                            The reason you got 2 as output is that you have given Items as a single string into your list, it means your list has single item of 'ABC, GHI, JKL' and then you append your second list item 'MNO'. Meanwhile should you correct your list at first line, sprating each item using , or using ' within , sperators.



                            my_list = ['ABC', 'GHI', 'JKL']
                            len(my_list)
                            3
                            my_list.append('MNO')
                            len(my_list)
                            4





                            share|improve this answer













                            Your code absolutly works correct,
                            The reason you got 2 as output is that you have given Items as a single string into your list, it means your list has single item of 'ABC, GHI, JKL' and then you append your second list item 'MNO'. Meanwhile should you correct your list at first line, sprating each item using , or using ' within , sperators.



                            my_list = ['ABC', 'GHI', 'JKL']
                            len(my_list)
                            3
                            my_list.append('MNO')
                            len(my_list)
                            4






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 25 '18 at 2:42









                            Ashkan KamyabAshkan Kamyab

                            702




                            702






























                                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%2f53464076%2fmy-code-will-append-a-list-but-not-print-out-the-correct-length%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

                                404 Error Contact Form 7 ajax form submitting

                                How to know if a Active Directory user can login interactively

                                Refactoring coordinates for Minecraft Pi buildings written in Python