(I'm sure this has been answered somewhere but I really couldn't find the right question. Perhaps I don't know the correct verb for this exercise?)
I have two lists:
prefix = ['A', 'B', 'C']
suffix = ['a', 'b']
And I want to get this:
output = ['A a', 'A b', 'B a', 'B b', 'C a', 'C b']
I am aware of the zip
method, which stops at the shortest length among the lists joined:
output_wrong = [p+' '+s for p,s in zip(prefix,suffix)]
So what's the most Pythonic way of doing this?
EDIT:
While majority of the answers prefer itertools.product
, I instead much prefer this:
output = [i + ' ' + j for i in prefix for j in suffix]
as it doesn't introduce a new package, however basic that package is (ok I don't know which way is faster and this might be a matter of personal preference).