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

Is there a way to simplify this function using a one-line comprehension in python?

simple question, as the title implies. I was hoping to use the zip function but can’t get it to work for some reason.

def tuple_sum(A, B):
out = []
for a,b in [x for x in zip(A,B)]:
    out1 = []
    for a1, b1 in zip(a, b):
        out1.append(a1+b1)
    out.append(out1)
return out

>Solution :

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

Maybe something like this?

A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
s = [[a1 + b1 for a1, b1 in zip(a, b)] for a, b in zip(A, B)]
print(s)  # [[6, 8], [10, 12]]
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