Length of a single sublist in python












1















Lets assume i have a list of 3 sublists



a = [[1,1,1],[1,1,1],[1,1,1]]


If I use the command len(a[0:2])
I get the answer 2 (Because there are two elements(sublists) in the list)



But when I do len(a[2]) and want to get the answer 1 (because there is only one element(sublist) in the list) I actually get the length of the third list (which is 3 in this case).



How could I solve this problem?










share|improve this question























  • How about len(a[2:3])?

    – Loocid
    Nov 23 '18 at 1:13













  • len(a[2]) takes the length of the third element. len(a[0:2]) takes the number of elements in that range.

    – Cua
    Nov 23 '18 at 2:07
















1















Lets assume i have a list of 3 sublists



a = [[1,1,1],[1,1,1],[1,1,1]]


If I use the command len(a[0:2])
I get the answer 2 (Because there are two elements(sublists) in the list)



But when I do len(a[2]) and want to get the answer 1 (because there is only one element(sublist) in the list) I actually get the length of the third list (which is 3 in this case).



How could I solve this problem?










share|improve this question























  • How about len(a[2:3])?

    – Loocid
    Nov 23 '18 at 1:13













  • len(a[2]) takes the length of the third element. len(a[0:2]) takes the number of elements in that range.

    – Cua
    Nov 23 '18 at 2:07














1












1








1








Lets assume i have a list of 3 sublists



a = [[1,1,1],[1,1,1],[1,1,1]]


If I use the command len(a[0:2])
I get the answer 2 (Because there are two elements(sublists) in the list)



But when I do len(a[2]) and want to get the answer 1 (because there is only one element(sublist) in the list) I actually get the length of the third list (which is 3 in this case).



How could I solve this problem?










share|improve this question














Lets assume i have a list of 3 sublists



a = [[1,1,1],[1,1,1],[1,1,1]]


If I use the command len(a[0:2])
I get the answer 2 (Because there are two elements(sublists) in the list)



But when I do len(a[2]) and want to get the answer 1 (because there is only one element(sublist) in the list) I actually get the length of the third list (which is 3 in this case).



How could I solve this problem?







python list sublist






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 1:09









Kaur KadakKaur Kadak

143




143













  • How about len(a[2:3])?

    – Loocid
    Nov 23 '18 at 1:13













  • len(a[2]) takes the length of the third element. len(a[0:2]) takes the number of elements in that range.

    – Cua
    Nov 23 '18 at 2:07



















  • How about len(a[2:3])?

    – Loocid
    Nov 23 '18 at 1:13













  • len(a[2]) takes the length of the third element. len(a[0:2]) takes the number of elements in that range.

    – Cua
    Nov 23 '18 at 2:07

















How about len(a[2:3])?

– Loocid
Nov 23 '18 at 1:13







How about len(a[2:3])?

– Loocid
Nov 23 '18 at 1:13















len(a[2]) takes the length of the third element. len(a[0:2]) takes the number of elements in that range.

– Cua
Nov 23 '18 at 2:07





len(a[2]) takes the length of the third element. len(a[0:2]) takes the number of elements in that range.

– Cua
Nov 23 '18 at 2:07












3 Answers
3






active

oldest

votes


















0














You need to use len(a[2:3]):



 a = [[1,2,3],[4,5,6],[7,8,9]]

a[2]
>>> [7, 8, 9]

len(a[2])
>>> 3

a[2:3]
>>> [[7, 8, 9]]

len(a[2:3])
>>> 1





share|improve this answer



















  • 1





    Thank, that works perfectly! I thought that len(a[2:3]) would be out of the list range because the list doesn't have anything on index 3 but I was mistaken.

    – Kaur Kadak
    Nov 23 '18 at 14:09



















0














You have to specify the range you want to look at.



len(a[1:2]) should do the trick.



len(a[x:y]) simply means "the length of a from element x to y (non-inclusive)"



So if you do len(a[2:2]) the output is 0.






share|improve this answer































    0














    You can use:



    print(len(a[2:3]))


    Or if want it builtin, do a function:



    _len=len
    def len(l):
    if _len(l)==0:
    return 0
    elif isinstance(l[0],list):
    return _len(l)
    return _len([l])

    a=[[1,1,1],[1,1,1],[1,1,1]]
    print(len(a[2]))


    Both Output:



    1





    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%2f53439591%2flength-of-a-single-sublist-in-python%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














      You need to use len(a[2:3]):



       a = [[1,2,3],[4,5,6],[7,8,9]]

      a[2]
      >>> [7, 8, 9]

      len(a[2])
      >>> 3

      a[2:3]
      >>> [[7, 8, 9]]

      len(a[2:3])
      >>> 1





      share|improve this answer



















      • 1





        Thank, that works perfectly! I thought that len(a[2:3]) would be out of the list range because the list doesn't have anything on index 3 but I was mistaken.

        – Kaur Kadak
        Nov 23 '18 at 14:09
















      0














      You need to use len(a[2:3]):



       a = [[1,2,3],[4,5,6],[7,8,9]]

      a[2]
      >>> [7, 8, 9]

      len(a[2])
      >>> 3

      a[2:3]
      >>> [[7, 8, 9]]

      len(a[2:3])
      >>> 1





      share|improve this answer



















      • 1





        Thank, that works perfectly! I thought that len(a[2:3]) would be out of the list range because the list doesn't have anything on index 3 but I was mistaken.

        – Kaur Kadak
        Nov 23 '18 at 14:09














      0












      0








      0







      You need to use len(a[2:3]):



       a = [[1,2,3],[4,5,6],[7,8,9]]

      a[2]
      >>> [7, 8, 9]

      len(a[2])
      >>> 3

      a[2:3]
      >>> [[7, 8, 9]]

      len(a[2:3])
      >>> 1





      share|improve this answer













      You need to use len(a[2:3]):



       a = [[1,2,3],[4,5,6],[7,8,9]]

      a[2]
      >>> [7, 8, 9]

      len(a[2])
      >>> 3

      a[2:3]
      >>> [[7, 8, 9]]

      len(a[2:3])
      >>> 1






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 23 '18 at 1:18









      Julian PellerJulian Peller

      9041512




      9041512








      • 1





        Thank, that works perfectly! I thought that len(a[2:3]) would be out of the list range because the list doesn't have anything on index 3 but I was mistaken.

        – Kaur Kadak
        Nov 23 '18 at 14:09














      • 1





        Thank, that works perfectly! I thought that len(a[2:3]) would be out of the list range because the list doesn't have anything on index 3 but I was mistaken.

        – Kaur Kadak
        Nov 23 '18 at 14:09








      1




      1





      Thank, that works perfectly! I thought that len(a[2:3]) would be out of the list range because the list doesn't have anything on index 3 but I was mistaken.

      – Kaur Kadak
      Nov 23 '18 at 14:09





      Thank, that works perfectly! I thought that len(a[2:3]) would be out of the list range because the list doesn't have anything on index 3 but I was mistaken.

      – Kaur Kadak
      Nov 23 '18 at 14:09













      0














      You have to specify the range you want to look at.



      len(a[1:2]) should do the trick.



      len(a[x:y]) simply means "the length of a from element x to y (non-inclusive)"



      So if you do len(a[2:2]) the output is 0.






      share|improve this answer




























        0














        You have to specify the range you want to look at.



        len(a[1:2]) should do the trick.



        len(a[x:y]) simply means "the length of a from element x to y (non-inclusive)"



        So if you do len(a[2:2]) the output is 0.






        share|improve this answer


























          0












          0








          0







          You have to specify the range you want to look at.



          len(a[1:2]) should do the trick.



          len(a[x:y]) simply means "the length of a from element x to y (non-inclusive)"



          So if you do len(a[2:2]) the output is 0.






          share|improve this answer













          You have to specify the range you want to look at.



          len(a[1:2]) should do the trick.



          len(a[x:y]) simply means "the length of a from element x to y (non-inclusive)"



          So if you do len(a[2:2]) the output is 0.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 1:16









          bunbunbunbun

          2,04532448




          2,04532448























              0














              You can use:



              print(len(a[2:3]))


              Or if want it builtin, do a function:



              _len=len
              def len(l):
              if _len(l)==0:
              return 0
              elif isinstance(l[0],list):
              return _len(l)
              return _len([l])

              a=[[1,1,1],[1,1,1],[1,1,1]]
              print(len(a[2]))


              Both Output:



              1





              share|improve this answer




























                0














                You can use:



                print(len(a[2:3]))


                Or if want it builtin, do a function:



                _len=len
                def len(l):
                if _len(l)==0:
                return 0
                elif isinstance(l[0],list):
                return _len(l)
                return _len([l])

                a=[[1,1,1],[1,1,1],[1,1,1]]
                print(len(a[2]))


                Both Output:



                1





                share|improve this answer


























                  0












                  0








                  0







                  You can use:



                  print(len(a[2:3]))


                  Or if want it builtin, do a function:



                  _len=len
                  def len(l):
                  if _len(l)==0:
                  return 0
                  elif isinstance(l[0],list):
                  return _len(l)
                  return _len([l])

                  a=[[1,1,1],[1,1,1],[1,1,1]]
                  print(len(a[2]))


                  Both Output:



                  1





                  share|improve this answer













                  You can use:



                  print(len(a[2:3]))


                  Or if want it builtin, do a function:



                  _len=len
                  def len(l):
                  if _len(l)==0:
                  return 0
                  elif isinstance(l[0],list):
                  return _len(l)
                  return _len([l])

                  a=[[1,1,1],[1,1,1],[1,1,1]]
                  print(len(a[2]))


                  Both Output:



                  1






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 1:17









                  U9-ForwardU9-Forward

                  16.6k51543




                  16.6k51543






























                      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%2f53439591%2flength-of-a-single-sublist-in-python%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

                      If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                      Alcedinidae

                      Can an atomic nucleus contain both particles and antiparticles? [duplicate]