Search code examples
pythondata-analysis

Summing Row Value in Nested List


I just started learning python, And got stuck in this question:

I have this list :

data = [
    ['Grinning', 2.26, 1.02, 87.3],
    ['Beaming', 19.1, 1.69, 150.0],
    ['ROFL', 25.6, 0.774, 0.0],
    ['Tears of Joy', 233.0, 7.31, 2270.0],
    ['Winking', 15.2, 2.36, 264.0],
    ['Happy', 22.7, 4.26, 565.0],
    ['Heart Eyes', 64.6, 11.2, 834.0],
    ['Kissing', 87.5, 5.13, 432.0],
    ['Thinking', 6.81, 0.636, 0.0],
    ['Unamused', 6.0, 0.236, 478.0],
    ['Sunglasses', 4.72, 3.93, 198.0],
    ['Loudly Crying', 24.7, 1.35, 654.0],
    ['Kiss Mark', 21.7, 2.87, 98.7],
    ['Two Hearts', 10.0, 5.69, 445.0],
    ['Heart', 118.0, 26.0, 1080.0],
    ['Heart Suit', 3.31, 1.82, 697.0],
    ['Thumbs Up', 23.1, 3.75, 227.0],
    ['Shrugging', 1.74, 0.11, 0.0],
    ['Fire', 4.5, 2.49, 150.0],
    ['Recycle', 0.0333, 0.056, 932.0]
]

I need to sum up each row of numbers, for example:

"Grinning" sum = 2.26 + 1.02 + 87.3

I tried a lot of ways to get only the row numbers But didn't found a way that would work correctly and will not sum up all of the columns

I tried this also:

totalemoji = 0
for i in range(len(data)):
    for i in range(1, 3):
        totalemoji += data[1][i] + data[1][i] + data[1][i]
print(totalemoji)

but it prints a number that I don't even recognize...

I would appreciate if someone can explain how to sum each row numbers


Solution

  • Option 1: Without sum as Requested

    d = {}
    
    for current_sub_list in data:
        current_list_sum = 0
        for j in range(1, len(current_sub_list)):
            current_list_sum += current_sub_list[j]
        d[current_sub_list[0]] = current_list_sum
    
    print(d)
    

    Option 2:

    d = {}
    
    for x in data:
        d[x[0]] = sum(x[1:])
    
    print(d)