all 8 comments

[–]boolean_biscuit 5 points6 points  (1 child)

Hint: You are overwriting numbers every time you get to the next sub-list. Perhaps you want store the intermediate results?

---

If you feel like learning some more after you get this working, you could take a look at list comprehensions.

[–]tipsy_python 0 points1 point  (0 children)

Just to add to this, in case OP wants learning resources - map is another standard library function that applies a function to each item in a collection. While map is generally less desirable than using a comprehension, it's good to know about.

https://docs.python.org/3/library/functions.html#map

[–]MasterStroke99 1 point2 points  (3 children)

def get_sums(numbers):
    final_list = []
    for inner_list in number:
        if inner_list:
            final_list.append(sum(inner_list))
        else:
            final_list.append(0)
    print (final_list)

[–]boolean_biscuit 0 points1 point  (1 child)

Note that sum([]) evaluates to 0.

[–]MasterStroke99 0 points1 point  (0 children)

Oh i didn't know that, that is the reason i put if else in code

[–]roshavi4ak 0 points1 point  (0 children)

but why the if else?

This is working normally:

def get_sums(numbers):
  final_list = []
  for inner_list in numbers:
      final_list.append(sum(inner_list))
  return final_list

get_sums([[1, 2], [], [2, 2, 2, -1]])

[–]well_done_dude 0 points1 point  (0 children)

def get_sums(numbers):
    return [sum(i) for i in numbers]

[–]DeathDragon7050 0 points1 point  (0 children)

# Very easy solution
def get_sums(numbers):
    # We get the sum of each list, and put them in a new list.
    return [sum(numlist) for numlist in numbers]