How to create a two dimensional list using for loop in Scala?











up vote
0
down vote

favorite












If I write the following code in scala I get one dimensional list, as such:



scala> for (a <- (1 to 2).toList; b <- (1 to 3).toList) yield (a, b)

res1 = List((1,1), (1,2), (1,3), (2,1), (2,2), (2,3))


But I'm expecting :-



List(List((1,1), (1,2), (1,3)), List((2,1), (2,2), (2,3)))


Is it possible to do this using a for loop in scala or is some kind of other construct needed?










share|improve this question









New contributor




Joe Johnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    If I write the following code in scala I get one dimensional list, as such:



    scala> for (a <- (1 to 2).toList; b <- (1 to 3).toList) yield (a, b)

    res1 = List((1,1), (1,2), (1,3), (2,1), (2,2), (2,3))


    But I'm expecting :-



    List(List((1,1), (1,2), (1,3)), List((2,1), (2,2), (2,3)))


    Is it possible to do this using a for loop in scala or is some kind of other construct needed?










    share|improve this question









    New contributor




    Joe Johnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      If I write the following code in scala I get one dimensional list, as such:



      scala> for (a <- (1 to 2).toList; b <- (1 to 3).toList) yield (a, b)

      res1 = List((1,1), (1,2), (1,3), (2,1), (2,2), (2,3))


      But I'm expecting :-



      List(List((1,1), (1,2), (1,3)), List((2,1), (2,2), (2,3)))


      Is it possible to do this using a for loop in scala or is some kind of other construct needed?










      share|improve this question









      New contributor




      Joe Johnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      If I write the following code in scala I get one dimensional list, as such:



      scala> for (a <- (1 to 2).toList; b <- (1 to 3).toList) yield (a, b)

      res1 = List((1,1), (1,2), (1,3), (2,1), (2,2), (2,3))


      But I'm expecting :-



      List(List((1,1), (1,2), (1,3)), List((2,1), (2,2), (2,3)))


      Is it possible to do this using a for loop in scala or is some kind of other construct needed?







      scala






      share|improve this question









      New contributor




      Joe Johnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Joe Johnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Nov 10 at 17:31





















      New contributor




      Joe Johnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 10 at 16:04









      Joe Johnes

      33




      33




      New contributor




      Joe Johnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Joe Johnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Joe Johnes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You could do it in 2 for comprehensions:



          for (n <- (1 to 4).toList) yield (for (m <- ('a' to 'c').toList) yield (n, m))





          share|improve this answer

















          • 1




            While this is the correct answer, I would like to point out that since in scala for/yield is not really "iterating", as the name may suggest to newcomers (specially for people coming from an OOP/Imperative background), and is just syntactic sugar for calls to map, flatMap & filter, @Tom's answer is also correct.
            – Luis Miguel Mejía Suárez
            Nov 10 at 17:48


















          up vote
          1
          down vote













          You could also use map



          (1 to 4).toList.map(n => ('a' to 'c').toList.map(m => (n, m)))






          share|improve this answer




























            up vote
            0
            down vote













            {for (i <- 1 to 2; j <- 1 to 3) yield (i,j)}.grouped(3).toList


            You get a List of Vector, but that's fine, Vector is usually preferred in most circumstances. Fast random access, append, prepend, and updates. You can forgo converting toList until the end. If you are OK with Vector Tom's answer is really good and you can just refactor down to:



            (1 to 4).map(n => ('a' to 'c').map(m => (n, m)))





            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',
              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
              });


              }
              });






              Joe Johnes is a new contributor. Be nice, and check out our Code of Conduct.










               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240777%2fhow-to-create-a-two-dimensional-list-using-for-loop-in-scala%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








              up vote
              1
              down vote



              accepted










              You could do it in 2 for comprehensions:



              for (n <- (1 to 4).toList) yield (for (m <- ('a' to 'c').toList) yield (n, m))





              share|improve this answer

















              • 1




                While this is the correct answer, I would like to point out that since in scala for/yield is not really "iterating", as the name may suggest to newcomers (specially for people coming from an OOP/Imperative background), and is just syntactic sugar for calls to map, flatMap & filter, @Tom's answer is also correct.
                – Luis Miguel Mejía Suárez
                Nov 10 at 17:48















              up vote
              1
              down vote



              accepted










              You could do it in 2 for comprehensions:



              for (n <- (1 to 4).toList) yield (for (m <- ('a' to 'c').toList) yield (n, m))





              share|improve this answer

















              • 1




                While this is the correct answer, I would like to point out that since in scala for/yield is not really "iterating", as the name may suggest to newcomers (specially for people coming from an OOP/Imperative background), and is just syntactic sugar for calls to map, flatMap & filter, @Tom's answer is also correct.
                – Luis Miguel Mejía Suárez
                Nov 10 at 17:48













              up vote
              1
              down vote



              accepted







              up vote
              1
              down vote



              accepted






              You could do it in 2 for comprehensions:



              for (n <- (1 to 4).toList) yield (for (m <- ('a' to 'c').toList) yield (n, m))





              share|improve this answer












              You could do it in 2 for comprehensions:



              for (n <- (1 to 4).toList) yield (for (m <- ('a' to 'c').toList) yield (n, m))






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 10 at 16:25









              Harald

              3,71221727




              3,71221727








              • 1




                While this is the correct answer, I would like to point out that since in scala for/yield is not really "iterating", as the name may suggest to newcomers (specially for people coming from an OOP/Imperative background), and is just syntactic sugar for calls to map, flatMap & filter, @Tom's answer is also correct.
                – Luis Miguel Mejía Suárez
                Nov 10 at 17:48














              • 1




                While this is the correct answer, I would like to point out that since in scala for/yield is not really "iterating", as the name may suggest to newcomers (specially for people coming from an OOP/Imperative background), and is just syntactic sugar for calls to map, flatMap & filter, @Tom's answer is also correct.
                – Luis Miguel Mejía Suárez
                Nov 10 at 17:48








              1




              1




              While this is the correct answer, I would like to point out that since in scala for/yield is not really "iterating", as the name may suggest to newcomers (specially for people coming from an OOP/Imperative background), and is just syntactic sugar for calls to map, flatMap & filter, @Tom's answer is also correct.
              – Luis Miguel Mejía Suárez
              Nov 10 at 17:48




              While this is the correct answer, I would like to point out that since in scala for/yield is not really "iterating", as the name may suggest to newcomers (specially for people coming from an OOP/Imperative background), and is just syntactic sugar for calls to map, flatMap & filter, @Tom's answer is also correct.
              – Luis Miguel Mejía Suárez
              Nov 10 at 17:48












              up vote
              1
              down vote













              You could also use map



              (1 to 4).toList.map(n => ('a' to 'c').toList.map(m => (n, m)))






              share|improve this answer

























                up vote
                1
                down vote













                You could also use map



                (1 to 4).toList.map(n => ('a' to 'c').toList.map(m => (n, m)))






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You could also use map



                  (1 to 4).toList.map(n => ('a' to 'c').toList.map(m => (n, m)))






                  share|improve this answer












                  You could also use map



                  (1 to 4).toList.map(n => ('a' to 'c').toList.map(m => (n, m)))







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 17:31









                  Tom

                  1,6042924




                  1,6042924






















                      up vote
                      0
                      down vote













                      {for (i <- 1 to 2; j <- 1 to 3) yield (i,j)}.grouped(3).toList


                      You get a List of Vector, but that's fine, Vector is usually preferred in most circumstances. Fast random access, append, prepend, and updates. You can forgo converting toList until the end. If you are OK with Vector Tom's answer is really good and you can just refactor down to:



                      (1 to 4).map(n => ('a' to 'c').map(m => (n, m)))





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        {for (i <- 1 to 2; j <- 1 to 3) yield (i,j)}.grouped(3).toList


                        You get a List of Vector, but that's fine, Vector is usually preferred in most circumstances. Fast random access, append, prepend, and updates. You can forgo converting toList until the end. If you are OK with Vector Tom's answer is really good and you can just refactor down to:



                        (1 to 4).map(n => ('a' to 'c').map(m => (n, m)))





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          {for (i <- 1 to 2; j <- 1 to 3) yield (i,j)}.grouped(3).toList


                          You get a List of Vector, but that's fine, Vector is usually preferred in most circumstances. Fast random access, append, prepend, and updates. You can forgo converting toList until the end. If you are OK with Vector Tom's answer is really good and you can just refactor down to:



                          (1 to 4).map(n => ('a' to 'c').map(m => (n, m)))





                          share|improve this answer












                          {for (i <- 1 to 2; j <- 1 to 3) yield (i,j)}.grouped(3).toList


                          You get a List of Vector, but that's fine, Vector is usually preferred in most circumstances. Fast random access, append, prepend, and updates. You can forgo converting toList until the end. If you are OK with Vector Tom's answer is really good and you can just refactor down to:



                          (1 to 4).map(n => ('a' to 'c').map(m => (n, m)))






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 10 at 18:07









                          Daniel Hinojosa

                          78237




                          78237






















                              Joe Johnes is a new contributor. Be nice, and check out our Code of Conduct.










                               

                              draft saved


                              draft discarded


















                              Joe Johnes is a new contributor. Be nice, and check out our Code of Conduct.













                              Joe Johnes is a new contributor. Be nice, and check out our Code of Conduct.












                              Joe Johnes is a new contributor. Be nice, and check out our Code of Conduct.















                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240777%2fhow-to-create-a-two-dimensional-list-using-for-loop-in-scala%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

                              Florida Star v. B. J. F.

                              Error while running script in elastic search , gateway timeout

                              Adding quotations to stringified JSON object values