[Python] How to find the length and sum of a list of lists in Python [closed]

Discussão em 'Python' iniciado por Stack, Agosto 16, 2025.

  1. Stack

    Stack Membro Participativo

    say I have a list x = [[1,2,3],[4,5],[6,7,8,9],[10]]

    what is the best way to find the length of the nth row of x?
    what is the best way to find the total length of x (i.e. the total number of elements in x, 10 in this case)?
    what is the best way to find the sum of the nth list in x?
    what is the best way to find the total sum of x (55 in this case)?

    len(x) gives 4, the number of rows in x
    sum(x) gives TypeError: unsupported operand type(s) for +: 'int' and 'list'
    in both cases I was hoping to return a list of ints (e.g. [3,2,4,1] for len(x))

    Continue reading...

Compartilhe esta Página