Let's say , I have following even number names list and should return representing pairs as result :
['A', 'B', 'C', 'D']
>>> [[('B', 'C'), ('A', 'D')],
[('A', 'B'), ('C', 'D')],
[('A', 'C'), ('B', 'D')]]
I wrote the following code :
import itertools
combinations = list(itertools.combinations(['A', 'B', 'C', 'D'], 2))
result = []
for i in range( 0 , len(combinations) ):
if (combinations[i-1][0] != combinations[i][0]) & (combinations[i-1][0] != combinations[i][1]) :
if (combinations[i-1][1] != combinations[i][0]) & (combinations[i-1][1] != combinations[i][1]) :
zipped = zip(combinations[i], combinations[i-1])
result.append(list(zipped))
result
But it gives following as a result ;
[[('A', 'C'), ('B', 'D')],
[('B', 'A'), ('C', 'D')]]
What is the missing point in my code ?