1

I am trying to create all combinations of two sets of lists using as follows:

x = [[1,2,3],[4,5,6]]
y = [['a','b','c'],['d','e','f']]

combos  = [[1,2,3,'a','b','c'],[4,5,6,'d','e','f'],[4,5,6,'a','b','c'],[4,5,6,'d','e','f']]

I think itertools may be of some help but not sure how. Thanks

1 Answer 1

4

You can use product and chain:

from itertools import product, chain
[list(chain(*i)) for i in product(x, y)]

#[[1, 2, 3, 'a', 'b', 'c'],
# [1, 2, 3, 'd', 'e', 'f'],
# [4, 5, 6, 'a', 'b', 'c'],
# [4, 5, 6, 'd', 'e', 'f']]

Or you can use a list comprehension:

[i + j for i in x for j in y]

#[[1, 2, 3, 'a', 'b', 'c'],
# [1, 2, 3, 'd', 'e', 'f'],
# [4, 5, 6, 'a', 'b', 'c'],
# [4, 5, 6, 'd', 'e', 'f']]
Sign up to request clarification or add additional context in comments.

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.