All pairs (as tuples) of integers adding to a given integer N

Question:

I want to store all pairs of integers whose sum is equal to N as tuples.
Here is my code so far:

m = []
l = []
degree = 9
for i in range(0, degree):
    m += [degree - i];
    l += [i]
    pairs = (m[i]),(l[i])    
pairs

This code return only last pair:

(1, 8)

What I want is this:

(9, 0),(8, 1),(7, 2),(6, 3),(5, 4),(4, 5),(3, 6),(2, 7),(1, 8)

Can you help me identify and fix the error in my code?

Asked By: feration48

||

Answers:

You can do it this way by defining a new pair’s list, appending it inside the loop, and finally printing.

m = []
l = []
degree = 9
pairs = []
for i in range(0, degree):
    m += [degree - i];
    l += [i]
    pairs.append((m[i],l[i]))
print(pairs)
Answered By: Always Sunny
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.