How to concatenate elements of multiple lists? [closed]












-2















There are 2 lists (but there can be many):



a = ['Sasha ','walked along ','the highway']
b = ['Masha ','ran on ','the road']


I need to display all options:




Sasha walked along the highway

Sasha walked along the road

Masha walked along the highway

Masha walked along the road

Sasha ran on the highway

Sasha ran on the road

Masha ran on the highway

Masha ran on the road











share|improve this question















closed as too broad by Alex, Rob, lagom, derloopkat, Mark Rotteveel Nov 24 '18 at 10:24


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.



















  • Please read How to Ask and Minimal, Complete, and Verifiable example.

    – Nic3500
    Nov 23 '18 at 18:47











  • Possible duplicate of How to extract the n-th elements from a list of tuples in python?

    – Alex
    Nov 23 '18 at 18:48
















-2















There are 2 lists (but there can be many):



a = ['Sasha ','walked along ','the highway']
b = ['Masha ','ran on ','the road']


I need to display all options:




Sasha walked along the highway

Sasha walked along the road

Masha walked along the highway

Masha walked along the road

Sasha ran on the highway

Sasha ran on the road

Masha ran on the highway

Masha ran on the road











share|improve this question















closed as too broad by Alex, Rob, lagom, derloopkat, Mark Rotteveel Nov 24 '18 at 10:24


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.



















  • Please read How to Ask and Minimal, Complete, and Verifiable example.

    – Nic3500
    Nov 23 '18 at 18:47











  • Possible duplicate of How to extract the n-th elements from a list of tuples in python?

    – Alex
    Nov 23 '18 at 18:48














-2












-2








-2


0






There are 2 lists (but there can be many):



a = ['Sasha ','walked along ','the highway']
b = ['Masha ','ran on ','the road']


I need to display all options:




Sasha walked along the highway

Sasha walked along the road

Masha walked along the highway

Masha walked along the road

Sasha ran on the highway

Sasha ran on the road

Masha ran on the highway

Masha ran on the road











share|improve this question
















There are 2 lists (but there can be many):



a = ['Sasha ','walked along ','the highway']
b = ['Masha ','ran on ','the road']


I need to display all options:




Sasha walked along the highway

Sasha walked along the road

Masha walked along the highway

Masha walked along the road

Sasha ran on the highway

Sasha ran on the road

Masha ran on the highway

Masha ran on the road








python string python-3.x list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 21:37









jpp

101k2162111




101k2162111










asked Nov 23 '18 at 18:43









WoodWood

22




22




closed as too broad by Alex, Rob, lagom, derloopkat, Mark Rotteveel Nov 24 '18 at 10:24


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as too broad by Alex, Rob, lagom, derloopkat, Mark Rotteveel Nov 24 '18 at 10:24


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • Please read How to Ask and Minimal, Complete, and Verifiable example.

    – Nic3500
    Nov 23 '18 at 18:47











  • Possible duplicate of How to extract the n-th elements from a list of tuples in python?

    – Alex
    Nov 23 '18 at 18:48



















  • Please read How to Ask and Minimal, Complete, and Verifiable example.

    – Nic3500
    Nov 23 '18 at 18:47











  • Possible duplicate of How to extract the n-th elements from a list of tuples in python?

    – Alex
    Nov 23 '18 at 18:48

















Please read How to Ask and Minimal, Complete, and Verifiable example.

– Nic3500
Nov 23 '18 at 18:47





Please read How to Ask and Minimal, Complete, and Verifiable example.

– Nic3500
Nov 23 '18 at 18:47













Possible duplicate of How to extract the n-th elements from a list of tuples in python?

– Alex
Nov 23 '18 at 18:48





Possible duplicate of How to extract the n-th elements from a list of tuples in python?

– Alex
Nov 23 '18 at 18:48












4 Answers
4






active

oldest

votes


















1














Using itertools.product with str.join:



from itertools import product

a = ['Sasha ','walked along ','the highway']
b = ['Masha ','ran on ','the road']

# option 1: list comprehension
res = [''.join(tup) for tup in product(*zip(a, b))]

# option 2: map
res = list(map(''.join, product(*zip(a, b))))

['Sasha walked along the highway',
'Sasha walked along the road',
'Sasha ran on the highway',
'Sasha ran on the road',
'Masha walked along the highway',
'Masha walked along the road',
'Masha ran on the highway',
'Masha ran on the road']





share|improve this answer

































    0














    Simply, without itertools and zip:



    def scr(*lists):

    rslt=[""]
    for idx in range(len(lists[0])): # all of the lists has the same length

    r=
    for s in rslt:
    for l in lists:
    r.append( s+l[idx] )
    rslt=r
    return rslt


    print(scr(a,b))





    share|improve this answer































      0














      Since you want all the options to be printed, you can try this:



      a = ['Sasha ','walked along ','the highway']
      b = ['Masha ','ran on ','the road']
      ab = [list(i) for i in zip(a,b)]
      for i in ab[0]:
      for j in ab[1]:
      for k in ab[2]:
      print(i,j,k)


      output:



      Sasha  walked along  the highway
      Sasha walked along the road
      Sasha ran on the highway
      Sasha ran on the road
      Masha walked along the highway
      Masha walked along the road
      Masha ran on the highway
      Masha ran on the road





      share|improve this answer


























      • I like this approach! Very simple me.

        – Wood
        Nov 23 '18 at 23:27



















      0














      Redid your version. Here's the right solution:



          a = ['Sasha ','Masha ']
      b = ['walked along ','ran on ']
      c = ['the highway','the road']

      ab = [list(i) for i in (a,b,c)]
      for x in ab[0]:
      for y in ab[1]:
      for z in ab[2]:
      print(x + y + z)

      Sasha walked along the highway
      Sasha walked along the road
      Sasha ran on the highway
      Sasha ran on the road
      Masha walked along the highway
      Masha walked along the road
      Masha ran on the highway
      Masha ran on the road





      share|improve this answer






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        Using itertools.product with str.join:



        from itertools import product

        a = ['Sasha ','walked along ','the highway']
        b = ['Masha ','ran on ','the road']

        # option 1: list comprehension
        res = [''.join(tup) for tup in product(*zip(a, b))]

        # option 2: map
        res = list(map(''.join, product(*zip(a, b))))

        ['Sasha walked along the highway',
        'Sasha walked along the road',
        'Sasha ran on the highway',
        'Sasha ran on the road',
        'Masha walked along the highway',
        'Masha walked along the road',
        'Masha ran on the highway',
        'Masha ran on the road']





        share|improve this answer






























          1














          Using itertools.product with str.join:



          from itertools import product

          a = ['Sasha ','walked along ','the highway']
          b = ['Masha ','ran on ','the road']

          # option 1: list comprehension
          res = [''.join(tup) for tup in product(*zip(a, b))]

          # option 2: map
          res = list(map(''.join, product(*zip(a, b))))

          ['Sasha walked along the highway',
          'Sasha walked along the road',
          'Sasha ran on the highway',
          'Sasha ran on the road',
          'Masha walked along the highway',
          'Masha walked along the road',
          'Masha ran on the highway',
          'Masha ran on the road']





          share|improve this answer




























            1












            1








            1







            Using itertools.product with str.join:



            from itertools import product

            a = ['Sasha ','walked along ','the highway']
            b = ['Masha ','ran on ','the road']

            # option 1: list comprehension
            res = [''.join(tup) for tup in product(*zip(a, b))]

            # option 2: map
            res = list(map(''.join, product(*zip(a, b))))

            ['Sasha walked along the highway',
            'Sasha walked along the road',
            'Sasha ran on the highway',
            'Sasha ran on the road',
            'Masha walked along the highway',
            'Masha walked along the road',
            'Masha ran on the highway',
            'Masha ran on the road']





            share|improve this answer















            Using itertools.product with str.join:



            from itertools import product

            a = ['Sasha ','walked along ','the highway']
            b = ['Masha ','ran on ','the road']

            # option 1: list comprehension
            res = [''.join(tup) for tup in product(*zip(a, b))]

            # option 2: map
            res = list(map(''.join, product(*zip(a, b))))

            ['Sasha walked along the highway',
            'Sasha walked along the road',
            'Sasha ran on the highway',
            'Sasha ran on the road',
            'Masha walked along the highway',
            'Masha walked along the road',
            'Masha ran on the highway',
            'Masha ran on the road']






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 23 '18 at 21:37

























            answered Nov 23 '18 at 20:34









            jppjpp

            101k2162111




            101k2162111

























                0














                Simply, without itertools and zip:



                def scr(*lists):

                rslt=[""]
                for idx in range(len(lists[0])): # all of the lists has the same length

                r=
                for s in rslt:
                for l in lists:
                r.append( s+l[idx] )
                rslt=r
                return rslt


                print(scr(a,b))





                share|improve this answer




























                  0














                  Simply, without itertools and zip:



                  def scr(*lists):

                  rslt=[""]
                  for idx in range(len(lists[0])): # all of the lists has the same length

                  r=
                  for s in rslt:
                  for l in lists:
                  r.append( s+l[idx] )
                  rslt=r
                  return rslt


                  print(scr(a,b))





                  share|improve this answer


























                    0












                    0








                    0







                    Simply, without itertools and zip:



                    def scr(*lists):

                    rslt=[""]
                    for idx in range(len(lists[0])): # all of the lists has the same length

                    r=
                    for s in rslt:
                    for l in lists:
                    r.append( s+l[idx] )
                    rslt=r
                    return rslt


                    print(scr(a,b))





                    share|improve this answer













                    Simply, without itertools and zip:



                    def scr(*lists):

                    rslt=[""]
                    for idx in range(len(lists[0])): # all of the lists has the same length

                    r=
                    for s in rslt:
                    for l in lists:
                    r.append( s+l[idx] )
                    rslt=r
                    return rslt


                    print(scr(a,b))






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 23 '18 at 21:26









                    kantalkantal

                    632127




                    632127























                        0














                        Since you want all the options to be printed, you can try this:



                        a = ['Sasha ','walked along ','the highway']
                        b = ['Masha ','ran on ','the road']
                        ab = [list(i) for i in zip(a,b)]
                        for i in ab[0]:
                        for j in ab[1]:
                        for k in ab[2]:
                        print(i,j,k)


                        output:



                        Sasha  walked along  the highway
                        Sasha walked along the road
                        Sasha ran on the highway
                        Sasha ran on the road
                        Masha walked along the highway
                        Masha walked along the road
                        Masha ran on the highway
                        Masha ran on the road





                        share|improve this answer


























                        • I like this approach! Very simple me.

                          – Wood
                          Nov 23 '18 at 23:27
















                        0














                        Since you want all the options to be printed, you can try this:



                        a = ['Sasha ','walked along ','the highway']
                        b = ['Masha ','ran on ','the road']
                        ab = [list(i) for i in zip(a,b)]
                        for i in ab[0]:
                        for j in ab[1]:
                        for k in ab[2]:
                        print(i,j,k)


                        output:



                        Sasha  walked along  the highway
                        Sasha walked along the road
                        Sasha ran on the highway
                        Sasha ran on the road
                        Masha walked along the highway
                        Masha walked along the road
                        Masha ran on the highway
                        Masha ran on the road





                        share|improve this answer


























                        • I like this approach! Very simple me.

                          – Wood
                          Nov 23 '18 at 23:27














                        0












                        0








                        0







                        Since you want all the options to be printed, you can try this:



                        a = ['Sasha ','walked along ','the highway']
                        b = ['Masha ','ran on ','the road']
                        ab = [list(i) for i in zip(a,b)]
                        for i in ab[0]:
                        for j in ab[1]:
                        for k in ab[2]:
                        print(i,j,k)


                        output:



                        Sasha  walked along  the highway
                        Sasha walked along the road
                        Sasha ran on the highway
                        Sasha ran on the road
                        Masha walked along the highway
                        Masha walked along the road
                        Masha ran on the highway
                        Masha ran on the road





                        share|improve this answer















                        Since you want all the options to be printed, you can try this:



                        a = ['Sasha ','walked along ','the highway']
                        b = ['Masha ','ran on ','the road']
                        ab = [list(i) for i in zip(a,b)]
                        for i in ab[0]:
                        for j in ab[1]:
                        for k in ab[2]:
                        print(i,j,k)


                        output:



                        Sasha  walked along  the highway
                        Sasha walked along the road
                        Sasha ran on the highway
                        Sasha ran on the road
                        Masha walked along the highway
                        Masha walked along the road
                        Masha ran on the highway
                        Masha ran on the road






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 24 '18 at 4:35

























                        answered Nov 23 '18 at 18:57









                        Sandesh34Sandesh34

                        254113




                        254113













                        • I like this approach! Very simple me.

                          – Wood
                          Nov 23 '18 at 23:27



















                        • I like this approach! Very simple me.

                          – Wood
                          Nov 23 '18 at 23:27

















                        I like this approach! Very simple me.

                        – Wood
                        Nov 23 '18 at 23:27





                        I like this approach! Very simple me.

                        – Wood
                        Nov 23 '18 at 23:27











                        0














                        Redid your version. Here's the right solution:



                            a = ['Sasha ','Masha ']
                        b = ['walked along ','ran on ']
                        c = ['the highway','the road']

                        ab = [list(i) for i in (a,b,c)]
                        for x in ab[0]:
                        for y in ab[1]:
                        for z in ab[2]:
                        print(x + y + z)

                        Sasha walked along the highway
                        Sasha walked along the road
                        Sasha ran on the highway
                        Sasha ran on the road
                        Masha walked along the highway
                        Masha walked along the road
                        Masha ran on the highway
                        Masha ran on the road





                        share|improve this answer




























                          0














                          Redid your version. Here's the right solution:



                              a = ['Sasha ','Masha ']
                          b = ['walked along ','ran on ']
                          c = ['the highway','the road']

                          ab = [list(i) for i in (a,b,c)]
                          for x in ab[0]:
                          for y in ab[1]:
                          for z in ab[2]:
                          print(x + y + z)

                          Sasha walked along the highway
                          Sasha walked along the road
                          Sasha ran on the highway
                          Sasha ran on the road
                          Masha walked along the highway
                          Masha walked along the road
                          Masha ran on the highway
                          Masha ran on the road





                          share|improve this answer


























                            0












                            0








                            0







                            Redid your version. Here's the right solution:



                                a = ['Sasha ','Masha ']
                            b = ['walked along ','ran on ']
                            c = ['the highway','the road']

                            ab = [list(i) for i in (a,b,c)]
                            for x in ab[0]:
                            for y in ab[1]:
                            for z in ab[2]:
                            print(x + y + z)

                            Sasha walked along the highway
                            Sasha walked along the road
                            Sasha ran on the highway
                            Sasha ran on the road
                            Masha walked along the highway
                            Masha walked along the road
                            Masha ran on the highway
                            Masha ran on the road





                            share|improve this answer













                            Redid your version. Here's the right solution:



                                a = ['Sasha ','Masha ']
                            b = ['walked along ','ran on ']
                            c = ['the highway','the road']

                            ab = [list(i) for i in (a,b,c)]
                            for x in ab[0]:
                            for y in ab[1]:
                            for z in ab[2]:
                            print(x + y + z)

                            Sasha walked along the highway
                            Sasha walked along the road
                            Sasha ran on the highway
                            Sasha ran on the road
                            Masha walked along the highway
                            Masha walked along the road
                            Masha ran on the highway
                            Masha ran on the road






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 24 '18 at 9:31









                            WoodWood

                            22




                            22















                                Popular posts from this blog

                                Costa Masnaga

                                Fotorealismo

                                Sidney Franklin