Issue 7176: sum() doesn't work for lists. (original) (raw)

Created on 2009-10-20 13:48 by oggust, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg94275 - (view) Author: Björn Augustsson (oggust) Date: 2009-10-20 13:48
Summary: "sum()" doesn't work on lists, even though the docs says it should. The docs say: "Note that sum(range(n), m) is equivalent to reduce(operator.add, range(n), m)" That's not true. -------------------------------- import operator a=[1,2] b=["x","y"] reduce(operator.add, [a,b]) # Works, gives "[1, 2, 'x', 'y']" as expected. sum ([a,b]) # Does not work, gives: "TypeError: unsupported operand type(s) for +: 'int' and 'list'" -------------------------------- (And "a + b" obviously works too.) /August.
msg94276 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-10-20 13:53
To sum lists, you need to supply a second argument to sum: >>> sum([[1, 2], ['x', 'y']], []) [1, 2, 'x', 'y'] The default value of the second argument is 0, which is why you're seeing the TypeError.
msg94295 - (view) Author: Björn Augustsson (oggust) Date: 2009-10-20 18:48
Er, that's fairly weird... Couldn't it do eg "type(first argument)", and if it's a list, replace the 0 with []? What's the second argument's use anyway? s = sum([1,2,3,4]) + 3 seems a lot clearer than s = sum([1,2,3,4], 3) and those seem to be equivalent. Thanks, /August.
msg94296 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-10-20 19:19
One use of the start argument is used to specify an initial zero/metpy value for the summation: 0 or 0.0 or Decimal(0) or []. BTW, sum() isn't a good technique for concatenating lists. Instead use something like: result = [] for seq in data: result.extend(seq) A fast one-liner version: result = list(itertools.chain.from_iterable(seq))
msg94506 - (view) Author: Björn Augustsson (oggust) Date: 2009-10-26 20:57
On Tue, Oct 20, 2009 at 20:19, Raymond Hettinger <report@bugs.python.org> wrote: > > Raymond Hettinger <rhettinger@users.sourceforge.net> added the comment: > > One use of the start argument is used to specify an initial zero/metpy > value for the summation:  0  or  0.0   or Decimal(0)  or  []. That means it's basically a way for you to tell sum() what the type of the first argument is. Which it could find out for itself. > BTW, sum() isn't a good technique for concatenating lists.  Instead use > something like: > >   result = [] >   for seq in data: >      result.extend(seq) > > A fast one-liner version: >   result = list(itertools.chain.from_iterable(seq)) IMHO there's no reason for sum() not to be as fast as possible at doing what it does. ("summing"). If it's somehow slower than the (way less readable) itertools incantation above, then I'd consider that to be a bug. /August. -- Wrong on most accounts. const Foo *foo; and Foo const *foo; mean the same: foo being a pointer to const Foo. const Foo const *foo; would mean the same but is illegal (double const). You are confusing this with Foo * const foo; and const Foo * const foo; respectively. -David Kastrup, comp.os.linux.development.system
History
Date User Action Args
2022-04-11 14:56:54 admin set github: 51425
2009-10-26 21:01:44 ezio.melotti set nosy: + ezio.melotti
2009-10-26 20:57:05 oggust set messages: +
2009-10-20 19:19:20 rhettinger set nosy: + rhettingermessages: +
2009-10-20 18:48:18 oggust set messages: +
2009-10-20 13:53:21 mark.dickinson set status: open -> closednosy: + mark.dickinsonmessages: + resolution: not a bug
2009-10-20 13:48:55 oggust create