How to print every nth index of a python list on a new line?












1















I'm trying to print out a list and for every 5 indexes, it prints a new line. So for example, if I have:



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


the output would be:



  1 2 3 4 5
6 7 8 9 10


I tried this so far:



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

for i in lst:
if len(lst) > 5:
print(lst,'n')


but all I get is:



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

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

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

.......


How could I do this?



Thanks for the help!










share|improve this question















migrated from superuser.com Jan 22 at 5:25


This question came from our site for computer enthusiasts and power users.



















  • What's your output if len(list) isn't divisible by your index?

    – Primusa
    Jan 22 at 5:46
















1















I'm trying to print out a list and for every 5 indexes, it prints a new line. So for example, if I have:



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


the output would be:



  1 2 3 4 5
6 7 8 9 10


I tried this so far:



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

for i in lst:
if len(lst) > 5:
print(lst,'n')


but all I get is:



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

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

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

.......


How could I do this?



Thanks for the help!










share|improve this question















migrated from superuser.com Jan 22 at 5:25


This question came from our site for computer enthusiasts and power users.



















  • What's your output if len(list) isn't divisible by your index?

    – Primusa
    Jan 22 at 5:46














1












1








1








I'm trying to print out a list and for every 5 indexes, it prints a new line. So for example, if I have:



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


the output would be:



  1 2 3 4 5
6 7 8 9 10


I tried this so far:



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

for i in lst:
if len(lst) > 5:
print(lst,'n')


but all I get is:



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

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

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

.......


How could I do this?



Thanks for the help!










share|improve this question
















I'm trying to print out a list and for every 5 indexes, it prints a new line. So for example, if I have:



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


the output would be:



  1 2 3 4 5
6 7 8 9 10


I tried this so far:



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

for i in lst:
if len(lst) > 5:
print(lst,'n')


but all I get is:



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

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

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

.......


How could I do this?



Thanks for the help!







python list printing split






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 22 at 5:48









Mehrdad Pedramfar

6,34411543




6,34411543










asked Jan 22 at 5:06









Landon GLandon G

12212




12212




migrated from superuser.com Jan 22 at 5:25


This question came from our site for computer enthusiasts and power users.









migrated from superuser.com Jan 22 at 5:25


This question came from our site for computer enthusiasts and power users.















  • What's your output if len(list) isn't divisible by your index?

    – Primusa
    Jan 22 at 5:46



















  • What's your output if len(list) isn't divisible by your index?

    – Primusa
    Jan 22 at 5:46

















What's your output if len(list) isn't divisible by your index?

– Primusa
Jan 22 at 5:46





What's your output if len(list) isn't divisible by your index?

– Primusa
Jan 22 at 5:46












4 Answers
4






active

oldest

votes


















2














Try this:



a = [1,2,3,4,5,6,7,8,9,10]
for i in [a[c:c+5] for c in range(0,len(a),5) if c%5 == 0]:
print(*i)


the output will be:



1 2 3 4 5
6 7 8 9 10


also you can replace 5 with any other number or variables.






share|improve this answer
























  • Hi, do you mind if I use the *i argument unpacking for the formatting in my answer? It's a lot better than the current " ".join() i've got going on

    – Primusa
    Jan 22 at 5:35













  • @Primusa Sure it is ok, I will also vote-up your answer.

    – Mehrdad Pedramfar
    Jan 22 at 5:36











  • No problem @Primusa, I think that is good if you look at sep= in print function too. it is good. something like this print(*i, sep='-') it will print: 1-2-3-4-5

    – Mehrdad Pedramfar
    Jan 22 at 5:44



















2














Used a for loop with a step:



n_indices = 5
lst = [1,2,3,4,5,6,7,8,9,10]

for i in range(0, len(lst), n_indices):
print(lst[i:i+n_indices])

>>>[1, 2, 3, 4, 5]
>>>[6, 7, 8, 9, 10]


If you want to be fancier with the formatting you can use argument unpacking like so: print(*list[i:i+n_indices]) and get outputs in this format:



1 2 3 4 5
6 7 8 9 10





share|improve this answer


























  • I guess he wants a formatted output only .join: print(' '.join(lst[i:i+indices]) ... will be good for him :)

    – Puneet Sinha
    Jan 22 at 5:43



















0














You Can use below code :-



for i in range(0,len(a),5):
print(" ".join(map(str, a[i:i+5])))


It will give you desired output.



Code Snip






share|improve this answer

































    0














    A bit more idiomatic than the other answers:



    n = 5
    lst = [1,2,3,4,5,6,7,8,9,10]

    for group in zip(*[iter(lst)] * n):
    print(*group)




    1 2 3 4 5
    6 7 8 9 10


    For larger lists, it's also much faster:



    In [1]: lst = range(1, 10001)

    In [2]: n = 5

    In [3]: %%timeit
    ...: for group in zip(*[iter(lst)] * n):
    ...: group
    ...:
    236 µs ± 49.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

    In [4]: %%timeit
    ...: for i in range(0, len(lst), n):
    ...: lst[i:i+n]
    ...:
    1.32 ms ± 184 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)





    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%2f54301753%2fhow-to-print-every-nth-index-of-a-python-list-on-a-new-line%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Try this:



      a = [1,2,3,4,5,6,7,8,9,10]
      for i in [a[c:c+5] for c in range(0,len(a),5) if c%5 == 0]:
      print(*i)


      the output will be:



      1 2 3 4 5
      6 7 8 9 10


      also you can replace 5 with any other number or variables.






      share|improve this answer
























      • Hi, do you mind if I use the *i argument unpacking for the formatting in my answer? It's a lot better than the current " ".join() i've got going on

        – Primusa
        Jan 22 at 5:35













      • @Primusa Sure it is ok, I will also vote-up your answer.

        – Mehrdad Pedramfar
        Jan 22 at 5:36











      • No problem @Primusa, I think that is good if you look at sep= in print function too. it is good. something like this print(*i, sep='-') it will print: 1-2-3-4-5

        – Mehrdad Pedramfar
        Jan 22 at 5:44
















      2














      Try this:



      a = [1,2,3,4,5,6,7,8,9,10]
      for i in [a[c:c+5] for c in range(0,len(a),5) if c%5 == 0]:
      print(*i)


      the output will be:



      1 2 3 4 5
      6 7 8 9 10


      also you can replace 5 with any other number or variables.






      share|improve this answer
























      • Hi, do you mind if I use the *i argument unpacking for the formatting in my answer? It's a lot better than the current " ".join() i've got going on

        – Primusa
        Jan 22 at 5:35













      • @Primusa Sure it is ok, I will also vote-up your answer.

        – Mehrdad Pedramfar
        Jan 22 at 5:36











      • No problem @Primusa, I think that is good if you look at sep= in print function too. it is good. something like this print(*i, sep='-') it will print: 1-2-3-4-5

        – Mehrdad Pedramfar
        Jan 22 at 5:44














      2












      2








      2







      Try this:



      a = [1,2,3,4,5,6,7,8,9,10]
      for i in [a[c:c+5] for c in range(0,len(a),5) if c%5 == 0]:
      print(*i)


      the output will be:



      1 2 3 4 5
      6 7 8 9 10


      also you can replace 5 with any other number or variables.






      share|improve this answer













      Try this:



      a = [1,2,3,4,5,6,7,8,9,10]
      for i in [a[c:c+5] for c in range(0,len(a),5) if c%5 == 0]:
      print(*i)


      the output will be:



      1 2 3 4 5
      6 7 8 9 10


      also you can replace 5 with any other number or variables.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 22 at 5:32









      Mehrdad PedramfarMehrdad Pedramfar

      6,34411543




      6,34411543













      • Hi, do you mind if I use the *i argument unpacking for the formatting in my answer? It's a lot better than the current " ".join() i've got going on

        – Primusa
        Jan 22 at 5:35













      • @Primusa Sure it is ok, I will also vote-up your answer.

        – Mehrdad Pedramfar
        Jan 22 at 5:36











      • No problem @Primusa, I think that is good if you look at sep= in print function too. it is good. something like this print(*i, sep='-') it will print: 1-2-3-4-5

        – Mehrdad Pedramfar
        Jan 22 at 5:44



















      • Hi, do you mind if I use the *i argument unpacking for the formatting in my answer? It's a lot better than the current " ".join() i've got going on

        – Primusa
        Jan 22 at 5:35













      • @Primusa Sure it is ok, I will also vote-up your answer.

        – Mehrdad Pedramfar
        Jan 22 at 5:36











      • No problem @Primusa, I think that is good if you look at sep= in print function too. it is good. something like this print(*i, sep='-') it will print: 1-2-3-4-5

        – Mehrdad Pedramfar
        Jan 22 at 5:44

















      Hi, do you mind if I use the *i argument unpacking for the formatting in my answer? It's a lot better than the current " ".join() i've got going on

      – Primusa
      Jan 22 at 5:35







      Hi, do you mind if I use the *i argument unpacking for the formatting in my answer? It's a lot better than the current " ".join() i've got going on

      – Primusa
      Jan 22 at 5:35















      @Primusa Sure it is ok, I will also vote-up your answer.

      – Mehrdad Pedramfar
      Jan 22 at 5:36





      @Primusa Sure it is ok, I will also vote-up your answer.

      – Mehrdad Pedramfar
      Jan 22 at 5:36













      No problem @Primusa, I think that is good if you look at sep= in print function too. it is good. something like this print(*i, sep='-') it will print: 1-2-3-4-5

      – Mehrdad Pedramfar
      Jan 22 at 5:44





      No problem @Primusa, I think that is good if you look at sep= in print function too. it is good. something like this print(*i, sep='-') it will print: 1-2-3-4-5

      – Mehrdad Pedramfar
      Jan 22 at 5:44













      2














      Used a for loop with a step:



      n_indices = 5
      lst = [1,2,3,4,5,6,7,8,9,10]

      for i in range(0, len(lst), n_indices):
      print(lst[i:i+n_indices])

      >>>[1, 2, 3, 4, 5]
      >>>[6, 7, 8, 9, 10]


      If you want to be fancier with the formatting you can use argument unpacking like so: print(*list[i:i+n_indices]) and get outputs in this format:



      1 2 3 4 5
      6 7 8 9 10





      share|improve this answer


























      • I guess he wants a formatted output only .join: print(' '.join(lst[i:i+indices]) ... will be good for him :)

        – Puneet Sinha
        Jan 22 at 5:43
















      2














      Used a for loop with a step:



      n_indices = 5
      lst = [1,2,3,4,5,6,7,8,9,10]

      for i in range(0, len(lst), n_indices):
      print(lst[i:i+n_indices])

      >>>[1, 2, 3, 4, 5]
      >>>[6, 7, 8, 9, 10]


      If you want to be fancier with the formatting you can use argument unpacking like so: print(*list[i:i+n_indices]) and get outputs in this format:



      1 2 3 4 5
      6 7 8 9 10





      share|improve this answer


























      • I guess he wants a formatted output only .join: print(' '.join(lst[i:i+indices]) ... will be good for him :)

        – Puneet Sinha
        Jan 22 at 5:43














      2












      2








      2







      Used a for loop with a step:



      n_indices = 5
      lst = [1,2,3,4,5,6,7,8,9,10]

      for i in range(0, len(lst), n_indices):
      print(lst[i:i+n_indices])

      >>>[1, 2, 3, 4, 5]
      >>>[6, 7, 8, 9, 10]


      If you want to be fancier with the formatting you can use argument unpacking like so: print(*list[i:i+n_indices]) and get outputs in this format:



      1 2 3 4 5
      6 7 8 9 10





      share|improve this answer















      Used a for loop with a step:



      n_indices = 5
      lst = [1,2,3,4,5,6,7,8,9,10]

      for i in range(0, len(lst), n_indices):
      print(lst[i:i+n_indices])

      >>>[1, 2, 3, 4, 5]
      >>>[6, 7, 8, 9, 10]


      If you want to be fancier with the formatting you can use argument unpacking like so: print(*list[i:i+n_indices]) and get outputs in this format:



      1 2 3 4 5
      6 7 8 9 10






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 22 at 5:37

























      answered Jan 22 at 5:31









      PrimusaPrimusa

      8,0002932




      8,0002932













      • I guess he wants a formatted output only .join: print(' '.join(lst[i:i+indices]) ... will be good for him :)

        – Puneet Sinha
        Jan 22 at 5:43



















      • I guess he wants a formatted output only .join: print(' '.join(lst[i:i+indices]) ... will be good for him :)

        – Puneet Sinha
        Jan 22 at 5:43

















      I guess he wants a formatted output only .join: print(' '.join(lst[i:i+indices]) ... will be good for him :)

      – Puneet Sinha
      Jan 22 at 5:43





      I guess he wants a formatted output only .join: print(' '.join(lst[i:i+indices]) ... will be good for him :)

      – Puneet Sinha
      Jan 22 at 5:43











      0














      You Can use below code :-



      for i in range(0,len(a),5):
      print(" ".join(map(str, a[i:i+5])))


      It will give you desired output.



      Code Snip






      share|improve this answer






























        0














        You Can use below code :-



        for i in range(0,len(a),5):
        print(" ".join(map(str, a[i:i+5])))


        It will give you desired output.



        Code Snip






        share|improve this answer




























          0












          0








          0







          You Can use below code :-



          for i in range(0,len(a),5):
          print(" ".join(map(str, a[i:i+5])))


          It will give you desired output.



          Code Snip






          share|improve this answer















          You Can use below code :-



          for i in range(0,len(a),5):
          print(" ".join(map(str, a[i:i+5])))


          It will give you desired output.



          Code Snip







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 22 at 5:46









          Primusa

          8,0002932




          8,0002932










          answered Jan 22 at 5:44









          rachit17rachit17

          242




          242























              0














              A bit more idiomatic than the other answers:



              n = 5
              lst = [1,2,3,4,5,6,7,8,9,10]

              for group in zip(*[iter(lst)] * n):
              print(*group)




              1 2 3 4 5
              6 7 8 9 10


              For larger lists, it's also much faster:



              In [1]: lst = range(1, 10001)

              In [2]: n = 5

              In [3]: %%timeit
              ...: for group in zip(*[iter(lst)] * n):
              ...: group
              ...:
              236 µs ± 49.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

              In [4]: %%timeit
              ...: for i in range(0, len(lst), n):
              ...: lst[i:i+n]
              ...:
              1.32 ms ± 184 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)





              share|improve this answer






























                0














                A bit more idiomatic than the other answers:



                n = 5
                lst = [1,2,3,4,5,6,7,8,9,10]

                for group in zip(*[iter(lst)] * n):
                print(*group)




                1 2 3 4 5
                6 7 8 9 10


                For larger lists, it's also much faster:



                In [1]: lst = range(1, 10001)

                In [2]: n = 5

                In [3]: %%timeit
                ...: for group in zip(*[iter(lst)] * n):
                ...: group
                ...:
                236 µs ± 49.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

                In [4]: %%timeit
                ...: for i in range(0, len(lst), n):
                ...: lst[i:i+n]
                ...:
                1.32 ms ± 184 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)





                share|improve this answer




























                  0












                  0








                  0







                  A bit more idiomatic than the other answers:



                  n = 5
                  lst = [1,2,3,4,5,6,7,8,9,10]

                  for group in zip(*[iter(lst)] * n):
                  print(*group)




                  1 2 3 4 5
                  6 7 8 9 10


                  For larger lists, it's also much faster:



                  In [1]: lst = range(1, 10001)

                  In [2]: n = 5

                  In [3]: %%timeit
                  ...: for group in zip(*[iter(lst)] * n):
                  ...: group
                  ...:
                  236 µs ± 49.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

                  In [4]: %%timeit
                  ...: for i in range(0, len(lst), n):
                  ...: lst[i:i+n]
                  ...:
                  1.32 ms ± 184 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)





                  share|improve this answer















                  A bit more idiomatic than the other answers:



                  n = 5
                  lst = [1,2,3,4,5,6,7,8,9,10]

                  for group in zip(*[iter(lst)] * n):
                  print(*group)




                  1 2 3 4 5
                  6 7 8 9 10


                  For larger lists, it's also much faster:



                  In [1]: lst = range(1, 10001)

                  In [2]: n = 5

                  In [3]: %%timeit
                  ...: for group in zip(*[iter(lst)] * n):
                  ...: group
                  ...:
                  236 µs ± 49.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

                  In [4]: %%timeit
                  ...: for i in range(0, len(lst), n):
                  ...: lst[i:i+n]
                  ...:
                  1.32 ms ± 184 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 22 at 5:48

























                  answered Jan 22 at 5:39









                  Tomothy32Tomothy32

                  7,8461728




                  7,8461728






























                      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%2f54301753%2fhow-to-print-every-nth-index-of-a-python-list-on-a-new-line%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

                      Paul Cézanne

                      UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

                      Angular material date-picker (MatDatepicker) auto completes the date on focus out