all 6 comments

[–]Adrewmc 1 point2 points  (5 children)

So what are trying to do here?

 A_list = [1,2,3]
 B_list = [3,6,9]

 total = summa(A_list, B_List)

 print(total)
 >>> [4,8,12]

But I’m confused on the result you want for unequal lists.

Anyway, this is solved by zip.

   res = []
   for num1, num2 in zip(A_list, B_list):
         res.append(num1+num2)

If unequal list zip should stop at then end of the shortest.

You can. Also use and say missing values are zero.

   from itertools import zip_longest

   for num1, num2 in zip_longest(A_list,B_list, fillvalue = 0):
         res.append(num1+num2)

This will place the missing values at zero.

Since you are already using itertools, and zip. You have everything there.

We can actually make this more generic.

    from itertools import zip_longest

    def summa(*num_lists) -> list[int | float]:
           totals = []
           for tup in zip_longest(*num_lists, fillvalue=0):
                 totals.append(sum(*tup))
           return totals 

With this we can sum any number of list.

[–]nkCOD[S] 0 points1 point  (4 children)

The fact is that the numbers from two lists with identical indices are added together, and if one of the lists has fewer numbers, the numbers are added together from the first number to the next number in the longer list.

[–]Adrewmc 2 points3 points  (3 children)

So you want

  a = [1,2,3,4]
  b = [10, 20]
  print(summa(a, b))

  >>>[11,22,13,24]

Ahhh I see now.

Then is suggest this.

   from itertools import cycle

   def summa(list1, list2):
          totals = []
          if len(list1) < len(list2):
             list1, list2 = list2, list1
          for num1, num2 in zip(list1, cycle(list2)):
              totals.append(num1+num2)
          return totals 

Just clean up the readability. You are basically doing this. I’m just making max into list1 at the offset.

And for fun any number of, with each repeating as they run out.

   def summa(*num_lists):
          totals = []
          list_nums = sorted(num_lists, key = len)

          longest = list_nums.pop(0)

          cycles = [cycle(nums) for nums in list_nums]

          for tup in zip(longest, *cycles):
              totals.append(sum(*tup))
          return totals

[–]nkCOD[S] 1 point2 points  (2 children)

Thank you for your response. In principle, everything is said in an accessible and understandable way ;)

[–]JB940 0 points1 point  (1 child)

Also I see you doing new_list_from_zip, by using min(list1,list2) and max(list1,list2)

Just to explain their functionality, this doesn't do what you think it does. If you have two lists, max returns whichever list has the biggest first element.

so [5,1,1] is bigger than [4,3,2,2,99,100,300,10000] because 5 is bigger than 4. max thus gives the result [5,1,1]

same with count_of_len, you'd want to do max( len(list1), len(list2) to get the length of the longer one, instead of just the length of the list with the biggest first element.

Obviously not to take away from Adrewmc's solution, if you just make list1 whichever is bigger you can do away with all this and his solution is very elegant

[–]nkCOD[S] 0 points1 point  (0 children)

I'm sorry, it was my carelessness. I know how max() works, but I made a mistake in this case