2

Consider the two following string sequences:

Salutation = ["Hello", "Hi"]
Names = ["Alice", "Matt", "Franck", "Julia"]

I am looking for clean way to merge those sequences into

["Hello_Alice", "Hi_Alice", "Hello_Matt", "Hi_Matt", "Hello_Franck", "Hi_Franck", "Hello_Julia", "Hi_Julia"]

or with whatever separator.

The equivalent in R would be:

c(outer(Salutations, Names, paste, sep="_"))
0

2 Answers 2

9

itertools.product is what you're looking for

import itertools
output = ['_'.join(i) for i in itertools.product(Salutation, Names)]
#or whatever separator you want
Sign up to request clarification or add additional context in comments.

Comments

1

One way would be to use nested for loops like this:

l = []
for s in Salutation:
    for n in Names:
        l.append(s + "_" + n)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.