from itertools import product
teams = ['india', 'australia', 'new zealand']
word_and = ['and']
tmp = '%s %s %s'
items = [teams, word_and, teams]
print(list(tmp % a for a in list(product(*items))))
prints:
['india and india',
'india and australia',
'india and new zealand',
'australia and india',
'australia and australia',
'australia and new zealand',
'new zealand and india',
'new zealand and australia',
'new zealand and new zealand']
How to:
http://pythonfiddle.com/product-without-matching-duplicates
You should use itertools.combinations
like this
>>> from itertools import combinations
>>> teams = ['india', 'australia', 'new zealand']
>>> [" and ".join(items) for items in combinations(teams, r=2)]
['india and australia', 'india and new zealand', 'australia and new zealand']
But for this simple case, you can run two loops, like this
>>> ["%s and %s" % (t1, t2) for i, t1 in enumerate(teams) for t2 in teams[i + 1:]]
['india and australia', 'india and new zealand', 'australia and new zealand']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With