Is there any method from python standart libraries that gives us the possibility to know all the possible combinations between two elements of a list (pairs). I have already made this function myself, and it looks like this:
def allPossiblePairs(list):
l = len(list)
pfinal = l - 1 # retirar
pos2 = 0
sums = []
for pos in range(l):
element = list[pos]
pos2 = pos
while pos2 != pfinal:
pos2 += 1
tosum = list[pos2]
pair = tuple((element, tosum))
sums.append(pair)
return(sums)
list1 = [1, 2, 3, 4, 5]
print(allPossiblePairs(list1))
This function will return the following list of tuples: [(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]
Also, this is not efficient, it is quite slow because you have a while loop in a for loop, iterating through each element in the list (for) and doing it again removing the previous element (while). Is there any way to do this without having to create a new function?