According to your updated description if I understand correctly what you need is itertools.product
(in general itertools
module deals with permutations and combinations and the docs are excellent. For example:
s = list(range(1, 5)) # [a, b)
print(list(itertools.product(s, repeat=2)))
# [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]
Below some further code I've written a while ago making use itertools
for my own needs :) (note if you are in python2 uncomment the first line from __future__ import print_function, division
line.
# from __future__ import print_function, division
import itertools
def possible_pairs(s, mirrors=True):
if len(s) != len(set(s)):
raise ValueError('duplicates found, but not allowed')
return list(itertools.permutations(s, 2)) if mirrors else list(itertools.combinations(s, 2))
def possible_pairs_between_two_lists(s1, s2, mirrors=True):
if len(s1) != len(set(s1)) or len(s2) != len(set(s2)):
raise ValueError('duplicates found, but not allowed')
return list(itertools.product(s1, s2)) + list(itertools.product(s2, s1)) if mirrors else list(
itertools.product(s1, s2))
s = list(range(1, 5)) # [a, b)
print(possible_pairs(s))
# [(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]
print(possible_pairs(s, False))
# (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
print(possible_pairs_between_two_lists(s, s))
# [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4), (1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]
print(possible_pairs_between_two_lists(s, s, False))
# [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]