I have 2 lists of equal lengths as follows
JavaScript
x
4
1
list1 = ['23, 42, 52', '4, 3, 6']
2
list2 = ['foo', 'bar']
3
4
How do I make a single list with the desired outcome be:
JavaScript
1
2
1
list = ['23foo, 42foo, 52foo', '4bar, 3bar, 6bar']
2
I have tried:
JavaScript
1
2
1
list= [i + j for i, j in zip(list1, list2)]
2
but this gives an output:
JavaScript
1
2
1
list = ['23, 42, 52foo', '4, 3, 6bar']
2
Advertisement
Answer
You’ll want:
JavaScript
1
6
1
list3 = [', '.join((f"{i}{j}" for i in k.split(', '))) for k, j in zip(list1, list2)]
2
3
print(list3)
4
5
['23foo, 42foo, 52foo', '4bar, 3bar, 6bar']
6
Because you need to split up the first string before you can add the desired suffix to it. Then, re-join the split string together.
Also, try not to shadow list
, it’s a built-in, so list3
is a better name for it