[Tutor] Adding all numbers in a file or list

Shashwat Anand anand.shashwat at gmail.com
Tue Aug 24 18:36:09 CEST 2010


On Tue, Aug 24, 2010 at 9:53 PM, Nitin Das <nitin.162 at gmail.com> wrote:

> alternatively you can use the lambda , reduce function for summing up all
> the numbers in a list for e.g:-
>
> lis = [1,2,3,4,5]
>
> p = reduce(lambda x,y : x+y, lis)
>
> p will have the value = 15.
>

Another approach,

>>> lis = [1,2,3,4,5]
>>> reduce(operator.add, lis)
15

However use sum() for this, which is the most obvious way to do it.


>
> --nitin
>
> On Mon, Aug 23, 2010 at 9:05 PM, aug dawg <augdawg09 at gmail.com> wrote:
>
>> Oh okay, sorry about that.
>>
>> Thanks for the help!
>>
>>
>>
>> On Mon, Aug 23, 2010 at 11:33 AM, Sander Sweers <sander.sweers at gmail.com>wrote:
>>
>>> On 23 August 2010 17:24, aug dawg <augdawg09 at gmail.com> wrote:
>>> > So it's sum(list_name) ?
>>>
>>> Correct, but it is not limited to lists. Any itterable with
>>> ints/floats will do, for example a tuple is also accepted.
>>>
>>> Greets
>>> Sander
>>>
>>> PS: Please use reply to all so others on this list may benefit from
>>> the questions/answers ;-)
>>>
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
~l0nwlf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100824/2e62d34f/attachment-0001.html>


More information about the Tutor mailing list