Sum a list of numbers in Python

0 votes

 have a list of numbers such as [1,2,3,4,5...], and I want to calculate (1+2)/2 and for the second, (2+3)/2 and the third, (3+4)/2, and so on. How can I do that?

I would like to sum the first number with the second and divide it by 2, then sum the second with the third and divide by 2, and so on.

Also, how can I sum a list of numbers?

a = [1, 2, 3, 4, 5, ...]

Is it:

b = sum(a)
print b

to get one number?

This is not working for me.

Feb 14, 2022 in Python by Dev
• 6,000 points
1,910 views

1 answer to this question.

0 votes

Let us take a list of Numbers and generate the sum of the entire list by using sum

Numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print (sum(Numbers))

Output

55

Well, for sequence part of the question (1+2)/2, (2+3)/2, ..., (9+10)/2
use a generator and the formula (2*i-1)/2. 
dot is used here to make the values in decimal or floating point.
The first element needs to be skipped when generating the new list

Solution = [(2*i-1)/2. for i in Numbers[1:]]
print(Solution)

Output

[1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]
print (sum(Solution))

Output

49.5
answered Feb 14, 2022 by Nandini
• 5,480 points

Related Questions In Python

0 votes
1 answer

Sum a list of numbers in Python

So you want (element 0 + element ...READ MORE

answered Feb 14, 2022 in Python by CoolCoder
• 4,420 points
3,113 views
+1 vote
8 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
36,073 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
3,482 views
0 votes
1 answer

Sorting a list of strings in Python

Use the sort function mylist.sort() READ MORE

answered May 29, 2018 in Python by Nietzsche's daemon
• 4,260 points
667 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,466 views
0 votes
1 answer
0 votes
1 answer

Is there a label/goto in Python?

Since Python is a structured programming language, ...READ MORE

answered Feb 4, 2022 in Python by Nandini
• 5,480 points
777 views
0 votes
1 answer

How can I count the occurrences of a list item?

For Counting the occurrences there are many ...READ MORE

answered Feb 7, 2022 in Python by Nandini
• 5,480 points
584 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP