How To Find The Sum Of Elements In Nested List?
Below is the given list:
x = [[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60]]
What I need here is the sum of numbers of each nested list in a different list.
Just loop through the items.
First you loop through the internal lists:
for lst in x:
Then you sum the items in the list using the
total = sum(lst)
Together it looks like this:
new_list = list() for lst in x: lst = sum(lst) new_list.append(total) print(new_list)
Hope this helps.
Edit: I haven't used the sum method before. Which is why the two downvotes despite the program working fine.
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module