lista = [[1,2,3],[4,5,6],[7,8,9]]
print(lista)
def filas(lista):
res=[]
for elemento in lista:
x = sum(lista[elemento])
res.append(x)
print(res)
I need to sum the numbers of each row, and then print it as a list. It seems the problem I have is that I try to sum the sublists, instead of the numbers of each row.
Is this what you want to do?
The issue you are having, is that you are already iterating over the elements so it is unnecessary to use it as an index:
It is generally considered bad form but to iterate over indexes you would use:
However, without introducing any other modules, you can use
map()
or a simple list comprehension:Or
Do you want like this?