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 8456961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:41:03+00:00 2026-06-10T12:41:03+00:00

I am trying to understand the working of the built-in sum() function, but, the

  • 0

I am trying to understand the working of the built-in sum() function, but, the start parameter has evaporated my mind:

  1. a=[[1, 20], [2, 3]]
    b=[[[[[[1], 2], 3], 4], 5], 6]
    >>> sum(b,a)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: can only concatenate list (not "int") to list
    >>> sum(a,b)
    [[[[[[1], 2], 3], 4], 5], 6, 1, 20, 2, 3]
    
  2. >>> a=[1,2]
    >>> b=[3,4]
    >>> sum(a,b)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: can only concatenate list (not "int") to list
    >>> sum(b,a)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: can only concatenate list (not "int") to list
    

I am just dumbfounded by this and don’t have any idea what is happening. Here is what the python docs have to say: http://docs.python.org/library/functions.html#sum. This does not give any explanation on ‘what if the start is not a string and not an integer?’

  • 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-10T12:41:04+00:00Added an answer on June 10, 2026 at 12:41 pm

    Sum does something like this

    def sum(values, start = 0):
        total = start
        for value in values:
            total = total + value
        return total
    

    sum([1,2],[3,4]) expands something like [3,4] + 1 + 2, which you can see tries to add numbers and lists together.

    In order to use sum to produce lists, the values should be a list of lists, whereas start can be just a list. You’ll see in your failing examples that the list contains at least some ints, rather then all lists.

    The usual case where you might think of using sum with lists is to convert a list of lists into a list

    sum([[1,2],[3,4]], []) == [1,2,3,4]
    

    But really you shouldn’t do that, as it’ll be slow.

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

Sidebar

Related Questions

I'm trying to understand the signing process of .NET applications. I'm working with Visual
I'm trying to understand how Eval() works for a specific purpose. I'm working on
I'm trying to better understand the concept of 'autocommit' when working with a Postgres
I'm trying to get a one-to-many relationship working with grails/gorm. I don't understand how
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
I'm trying to understand the whole provisioning-proccess, but I just don't get it.. I
Im trying to start working with Windows Forms... And I tryed to make programm
This is a stupid example but I'm trying to understand how thing get passed
I'm trying to understand Google services and OAuth 2.0 while working on a Calendar
I am trying to understand the inner workings of queue (3) macros in Freebsd.

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.