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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:04:07+00:00 2026-05-16T07:04:07+00:00

Python has a built in function sum , which is effectively equivalent to: def

  • 0

Python has a built in function sum, which is effectively equivalent to:

def sum2(iterable, start=0):
    return start + reduce(operator.add, iterable)

for all types of parameters except strings. It works for numbers and lists, for example:

 sum([1,2,3], 0) = sum2([1,2,3],0) = 6    #Note: 0 is the default value for start, but I include it for clarity
 sum({888:1}, 0) = sum2({888:1},0) = 888

Why were strings specially left out?

 sum( ['foo','bar'], '') # TypeError: sum() can't sum strings [use ''.join(seq) instead]
 sum2(['foo','bar'], '') = 'foobar'

I seem to remember discussions in the Python list for the reason, so an explanation or a link to a thread explaining it would be fine.

Edit: I am aware that the standard way is to do "".join. My question is why the option of using sum for strings was banned, and no banning was there for, say, lists.

Edit 2: Although I believe this is not needed given all the good answers I got, the question is: Why does sum work on an iterable containing numbers or an iterable containing lists but not an iterable containing strings?

  • 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-16T07:04:07+00:00Added an answer on May 16, 2026 at 7:04 am

    Python tries to discourage you from “summing” strings. You’re supposed to join them:

    "".join(list_of_strings)
    

    It’s a lot faster, and uses much less memory.

    A quick benchmark:

    $ python -m timeit -s 'import operator; strings = ["a"]*10000' 'r = reduce(operator.add, strings)'
    100 loops, best of 3: 8.46 msec per loop
    $ python -m timeit -s 'import operator; strings = ["a"]*10000' 'r = "".join(strings)'
    1000 loops, best of 3: 296 usec per loop
    

    Edit (to answer OP’s edit): As to why strings were apparently “singled out”, I believe it’s simply a matter of optimizing for a common case, as well as of enforcing best practice: you can join strings much faster with ”.join, so explicitly forbidding strings on sum will point this out to newbies.

    BTW, this restriction has been in place “forever”, i.e., since the sum was added as a built-in function (rev. 32347)

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

Sidebar

Related Questions

My site, which is built on Python/Postgresql with django, has an area where you
python has a built-in function ord() , I think it must be a anbbreviation.
Does anyone know if Python has an in-built function to work to print out
pypy has many built-in function use python implementation.example: link but,i can't find model 'sys'
I have list in python which has following entries name-1 name-2 name-3 name-4 name-1
I recently discovered Python's property built-in , which disguises class method getters and setters
I know that Python has built-in list functions but I'm curious as to how
Is there a built-in function in Python that would replace (or remove, whatever) the
I'm attempting to build Python 2.6.2 from source on my Linux system. It has
Python has an ordered dictionary . What about an ordered set?

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.