Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

All sums combinations of N as tuple

I want to store all pairs which are sum of 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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

How to do it?

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading