1

(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).

data-monkey
  • 1,535
  • 3
  • 15
  • 24
  • I like how we got 4 answers of roughly the same thing within a minute. :D – Mateen Ulhaq Jun 27 '18 at 10:33
  • 1
    @MateenUlhaq There are hundred similar questions out there, instead of answering this, we should direct OP to one of them, but you know SO. – BcK Jun 27 '18 at 10:38
  • @BcK In fairness to myself, I've already hit the rep cap for the day and just wanted to write a `itertools` answer. ;) ...Though closing as duplicate was probably the more accurate thing to do. – Mateen Ulhaq Jun 27 '18 at 10:39
  • 3
    Possible duplicate of [Get the cartesian product of a series of lists?](https://stackoverflow.com/questions/533905/get-the-cartesian-product-of-a-series-of-lists) – Michael Kohl Jun 27 '18 at 10:40
  • As I said I probably didn't know the right word, in this case "cartesian product". Thank you guys for pointing this out. – data-monkey Jun 27 '18 at 10:42

7 Answers7

4

Use List Comprehension

prefix = ['A', 'B', 'C']
suffix = ['a', 'b']
result = [val+" "+val2 for val in prefix for val2 in suffix ]
print(result)

OUTPUT

['A a', 'A b', 'B a', 'B b', 'C a', 'C b']
sachin dubey
  • 755
  • 9
  • 28
3

Using itertools.product and list comprehension,

>>> [i + ' ' + j for i, j in product(prefix, suffix)]
# ['A a', 'A b', 'B a', 'B b', 'C a', 'C b']
BcK
  • 2,548
  • 1
  • 13
  • 27
2

Use itertools.product:

import itertools

prefix = ['A', 'B', 'C']
suffix = ['a', 'b']

print([f'{x} {y}' for x, y in itertools.product(prefix, suffix)])
# ['A a', 'A b', 'B a', 'B b', 'C a', 'C b']
Austin
  • 25,759
  • 4
  • 25
  • 48
1

This is called a Cartesian product:

[p + ' ' + s for p, s in itertools.product(prefix, suffix)]
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
1

Use product,

In [33]: from itertools import product

In [34]: map(lambda x:' '.join(x),product(prefix,suffix))
Out[34]: ['A a', 'A b', 'B a', 'B b', 'C a', 'C b']
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
1

Simply use list comprehension:

prefix = ['A', 'B', 'C']
suffix = ['a', 'b']
output = [i+" "+j for i in prefix for j in suffix]
print(output)

Output:

['A a', 'A b', 'B a', 'B b', 'C a', 'C b']
Taohidul Islam
  • 5,246
  • 3
  • 26
  • 39
0
from itertools import product
map(' '.join, product(prefix, suffix))
# ['A a', 'A b', 'B a', 'B b', 'C a', 'C b']
Sunitha
  • 11,777
  • 2
  • 20
  • 23