Function to Add List Elements?

Henry Chang goldspin at gmail.com
Wed Oct 22 22:22:01 CEST 2008


Very nice, that works!  Thanks so much, Chris!

On Wed, Oct 22, 2008 at 1:06 PM, Chris Rebert <clp at rebertia.com> wrote:

> On Wed, Oct 22, 2008 at 12:59 PM, Henry Chang <goldspin at gmail.com> wrote:
> > This seems like a simple problem, but I can't find a simple solution.
> >
> > Suppose I have two lists of integers.
> >
> > List A = [A1, A2, A3]
> > List B = [B1, B2, B3]
> >
> > I just simply want a new list, such as:
> >
> > List C = [C1, C2, C3]
> >
> > where:
> >
> > C1 = A1 + B1
> > C2 = A2 + B2
> > C3 = A3 + B3
> >
> > Is there a simple function to do this?
>
> A one-liner in fact:
>
> summed = [sum(pair) for pair in zip(listA, listB)]
>
> Explanation: pairs up the elements of listA and listB, sums each pair,
> constructs a list of the sums.
>
> Cheers,
> Chris
> --
> Follow the path of the Iguana...
> http://rebertia.com
>
> >
> > Thanks so much?
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081022/b3a82d8a/attachment.html>


More information about the Python-list mailing list