0

I want to challenge myself and I want to build a keyword combiner tool like these ones:

I would like to build this using Python but I don't really know how to combine the items from a list with another list:

a = ['blue', 'red', 'green']
b = ['shoes', 'shirt', 'pants']

c = a + b

And if I print c I get this:

['blue', 'red', 'green', 'shoes', 'shirt', 'pants']

But I want it to be combined as this:

 - blue shoes
 - red shoes 
 - green shoes
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

1 Answers1

-1

If you want to get a list containing all the possible combinations, this might be the fastest way

c = [(x+" "+y) for x in a for y in b]

It iterates through both list and writes all combinations in the new list.

In the end: c = ['blue shoes', 'blue shirt', 'blue pants', 'red shoes', 'red shirt', 'red pants', 'green shoes', 'green shirt', 'green pants']

fynsta
  • 348
  • 3
  • 10
  • I didn't downvote (this is correct...), but this is basically what [`product`](https://docs.python.org/3.7/library/itertools.html#itertools.product) does... – Tomerikoo Feb 06 '21 at 16:18
  • I understand, but it might be better not to import a custom module just for this one thing that can be done in a single line otherwise... – fynsta Feb 06 '21 at 16:48
  • `itertools` is a built-in library... Again, I didn't downvote and your answer is perfectly fine and correct... – Tomerikoo Feb 06 '21 at 16:51