I have a list of elements: list = ['A','B',C']. How do I iterate through this list and return the following: [AB, AC, BC]?
note: I only want unique pairs, not [AA, BB, CC...] or [AB, BA, BC, CB...]
CodePudding user response:
You need itertools.combinations
:
In [1]: from itertools import combinations
In [2]: for c in combinations(['A', 'B', 'C'], 2):
...: print(c)
...:
('A', 'B')
('A', 'C')
('B', 'C')
CodePudding user response:
you can do it this way
lst = ['A','B','C']
result=[]
for i in range(len(lst)):
for j in range(i 1,len(lst)):
result.append(lst[i] lst[j])