Matt Hellewell
Matt Hellewell

Reputation: 73

Python - How to sum a certain section of a list

I have a list of 90 values and I would like to find the value of the sum of the first 30 of these values. Is there a way I can do this easily?

Upvotes: 0

Views: 645

Answers (3)

Shubham Setia
Shubham Setia

Reputation: 11

Simplest way to do is slice the list and use sum function

ans = sum(input[:30])

If you want to add numbers between a particular range let's say you want to add numbers between 20-30(inclusive) then you can do that as

ans = sum(input[20:31])

Upvotes: 0

Mureinik
Mureinik

Reputation: 312289

The easiest would be to just slice it:

result = sum(lst[0:30])

Upvotes: 2

Saravana Kumar
Saravana Kumar

Reputation: 482

You can use the following command:

l = [1,2,3,4,5.....90]
s = sum(l[:30])

Upvotes: 2

Related Questions