How to concatenate elements of multiple lists? [closed]
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
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.
add a comment |
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
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
add a comment |
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
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
python string python-3.x list
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
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']
add a comment |
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))
add a comment |
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
I like this approach! Very simple me.
– Wood
Nov 23 '18 at 23:27
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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']
add a comment |
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']
add a comment |
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']
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']
edited Nov 23 '18 at 21:37
answered Nov 23 '18 at 20:34
jppjpp
101k2162111
101k2162111
add a comment |
add a comment |
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))
add a comment |
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))
add a comment |
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))
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))
answered Nov 23 '18 at 21:26
kantalkantal
632127
632127
add a comment |
add a comment |
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
I like this approach! Very simple me.
– Wood
Nov 23 '18 at 23:27
add a comment |
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
I like this approach! Very simple me.
– Wood
Nov 23 '18 at 23:27
add a comment |
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
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
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 24 '18 at 9:31
WoodWood
22
22
add a comment |
add a comment |
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