product
from itertools
will do the trick.
product(first, last)
will give return a generator with all possible combinations of first
and last
. After that, all you need to do is concatenate the first and last names. You can do this in one expression:
combined = [" ".join(pair) for pair in product(first, last)]
It's also possible to do this with string concatenation:
combined = [pair[0] + " " + pair[1] for pair in product(first, last)]
This method is slower though, as the concatenation done in the interpreter. It's always recommended to use the "".join()
method as this code is executed in C.