Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7576901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:02:37+00:00 2026-05-30T17:02:37+00:00

The closest was this one summing columns . So I’ll do something similar in

  • 0

The closest was this one summing columns.

So I’ll do something similar in my question:

Say I’ve a Python 2D list as below:

my_list =  [ [1,2,3,4],
             [2,4,5,6] ]

I can get the row totals with a list comprehension:

row_totals = [ sum(x) for x in my_list ]

In one line, how can I sum the entire 2d-array?

27
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:02:39+00:00Added an answer on May 30, 2026 at 5:02 pm

    You can do as easy as

    sum(map(sum, my_list))
    

    or alternatively

    sum(sum(x) for x in my_list))
    

    and call it a day, if you don’t expect more than 2 dimensions. Note that the first solution is most likely not the fastest (as in execution time) solution, due to the usage of map(). Benchmark and compare as necessary.

    Finally, if you find yourself using multidimensional arrays, consider using NumPy and its superior array-friendly functions. Here’s a short excerpt for your problem:

    import numpy as np
    
    my_list = np.array([[1,2,3,4], [2,4,5,6]])
    np.sum(my_list)
    

    This would work for any number of dimensions your arrays might have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The closest I could find to this question was this one: Getting all types
The closest question I found in SO was this one , but the answers
hope you can help me on this one, I'm currently using this: jQuery plugin:validation
I have a list of div's. This is one div structure: <div class=commentWrap id=@Model.CommentId>
I hope you can help me with this one. My application is monitoring a
I really searched on this one. (E/TextMate is the closest I found in this
I got one of these: $('a.retweet').click(function () { var retweetText = $(this).closest('.article-content').children('p').text(); } With
I'm wondering about how you would implement an effect similar to this one implemented
I have the following jQuery: $(this).closest('div.mcoup').find('div.delcoup').slideToggle(400) .siblings().children('div.delcoup').slideUp(400); What I'm trying to do is get
I have the following function. $(function() { $(.sectionHeader:gt(0)).click(function() { $(this).next(.fieldset).slideToggle(fast); }); $(img[alt='minimize']).click(function(e) { $(this).closest(table).next(.fieldset).slideUp(fast);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.