Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Combine Elements in one List With All Elements in Another

Given the following lists:

a = ['a','b']
b = [1,2,3,4]

I'd like to produce this:

c = ['a1','a2','a3','a4','b1','b2','b3','b4']

So I basically want to join every element of b to each element in a.

I'd like an approach similar to this:

[x+str(y) for x in a and y in b]

Thanks in advance!

like image 261
Dance Party2 Avatar asked Sep 13 '25 06:09

Dance Party2


1 Answers

a = ['a','b']
b = [1,2,3,4]
c = [x+str(y) for x in a for y in b]
print(c)

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!