sum for sequences?

Neil Cerutti neilc at norwich.edu
Wed Mar 24 11:42:02 EDT 2010


On 2010-03-24, kj <no.email at please.post> wrote:
>
>
> Is there a sequence-oriented equivalent to the sum built-in?  E.g.:
>
>   seq_sum(((1, 2), (5, 6))) --> (1, 2) + (5, 6) --> (1, 2, 5, 6)
>
> ?
>
> (By "sequence" I'm referring primarily to lists and tuples, and
> excluding strings, since for these there is ''.join()).

reduce, or functools.reduce in Python 3.1.

>>> functools.reduce(operator.add, ((1, 2), (5, 6)))
(1, 2, 5, 6)

-- 
Neil Cerutti
"It's not fun to build walls. But it's even less fun to live
without walls in a world full of zombies." --Greedy Goblin



More information about the Python-list mailing list