Given a list of length N in Python:
a = [A,B,C]
I want to create all possible combinations of lists with a set maximum length (in this case 2). The order of the elements also matters. This is what I would like as result:
[A,B],[C]
[B,A],[C]
[A,C],[B]
[C,A],[B]
[B,C],[A]
[C,B],[A]
I think the permutations function is a good place to start. But I can't figure out how to get the exact results.
Try this:
a = ['A', 'B', 'C']
for v in permutations(a):
for i in range(0, len(v), 2):
print v[i:i+2]
print '-'*10
Output:
('A', 'B')
('C',)
----------
('A', 'C')
('B',)
----------
('B', 'A')
('C',)
----------
('B', 'C')
('A',)
----------
('C', 'A')
('B',)
----------
('C', 'B')
('A',)
----------