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

  • Home
  • SEARCH
  • 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 8222571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:19:34+00:00 2026-06-07T14:19:34+00:00

I am adding up the elements from all of the nested lists, so far

  • 0

I am adding up the elements from all of the nested lists, so far what I have is

for i in range(len(b)) :
    for j in range(len(b[i])):
        total = total + b[i][j]

But this will not work when an integer value exists in the nested list like,

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

In this case i am getting an error message that object of type ‘int’ has no len().
what should i do in this case?

  • 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-06-07T14:19:36+00:00Added an answer on June 7, 2026 at 2:19 pm

    try something like this,
    use sum() only when the element is an list or tuple, else simple use the number.

    >>> b = [[1,2], [3,4], [5,6], 1]
    >>> sum(sum(x) if isinstance(x,(list,tuple)) else x for x in b)
    22
    

    improving your solution:

    total=0
    b = [[1,2], [3,4], [5,6], 1]
    for i in range(len(b)) :
        if isinstance(b[i],(list,tuple)):
            for j in range(len(b[i])):
                total += b[i][j]
        else:
            total +=b[i]
    print total  #prints 22
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I'm doing is adding a class to all elements who have the same
Possible Duplicate: Dynamically adding collapsible elements I've seen a few posts on this but
I want to remove the class attribute from all elements that have an empty
After adding elements to page using something like this, $('#blah').append('<div id=bugsbunny></div>'); how can I
I have a list and I'm adding elements using RPUSH which returns the updated
Am I right in assuming that adding/removing elements to an std::map does not effect
All my views/pages in apps so far have been full screen UIViewControllers that i
I have a jQuery AJAX data grid that loads a list of elements from
I am adding a remove me icon to all elements on my page with
Say I want to copy all elements of list A into list B. But

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.