from itertools import combinations l = [5,2,3,9,1] for var in combinations(l, 2): if var[0] + var[1] == 10: print var[0], var[1]
Combinations create all possible combinations of tuples from an tuples object (an object that you can iterate over). Let me demonstrate:
>>> [var for var in combinations([1,2,3,4,5], 2)] [(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)] >>> [var for var in combinations([1,2,3,4,5], 3)] [(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (1, 4, 5), (2, 3, 4), (2, 3, 5), (2, 4, 5), (3, 4, 5)]