0

I have an array, and it has multiple structures, with arrays inside arrays, how can I add all the numbers inside the main array?

arr = [
    [1, 2, 3],
    [
        4, 5, 6,
        [
            [7, 8, 9],
            11, 12, 13,
            [14, 15, 
                [
                    16, 17, 18
                ]
            ]
        ], 
        [19, 20]
    ]
]

I have to ignore the structure, because it's never the same

  • 1
    Although you don't have to flatten the list but idea is the same as flattening in the linked dupe but instead, you sum it. Possible implementation can be `def rec_sum(L): total=0; if isinstance(val, list): total+=rec_sum; else: total += val; return total` – Ch3steR Oct 26 '21 at 16:38
  • Online link: https://ideone.com/ITVOF8 – Ch3steR Oct 26 '21 at 16:44

0 Answers0