Sum a list of numbers in Python

0 votes

I 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 doesn't work for me.

Feb 14, 2022 in Python by surbhi
• 3,810 points
2,672 views

1 answer to this question.

0 votes

So you want (element 0 + element 1) / 2, (element 1 + element 2) / 2, and so on.

We make two lists: one with all of the elements except the first, and another with all of the elements except the last. The averages we're looking for are the averages of each pair from both lists. To create pairs from two lists, we use zip.

Even though your input numbers are integers, I assume you want decimals in the outcome. Python divides integers by default and discards the residue. Floating-point numbers are required to divide things all the way through. Since dividing an int by a float will produce a float, so we use 2.0 for our divisor instead of 2.

averages = [(x + y) / 2.0 in zip(my list[:-1], my list[1:]) for (x, y)]

2nd question:

That application of sum should be fine. The following are some examples of works:

a = range(10)
# [0,1,2,3,4,5,6,7,8,9]
b = sum(a)
print b
# Prints 45

In addition, you don't have to assign everything to a variable at every stage. print sum(a) works perfectly.

You'll need to be more explicit about what you wrote and why it didn't work.

answered Feb 14, 2022 by CoolCoder
• 4,400 points

Related Questions In Python

0 votes
1 answer

Sum a list of numbers in Python

Let us take a list of Numbers ...READ MORE

answered Feb 14, 2022 in Python by Nandini
• 5,480 points
1,550 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
35,175 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,226 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
431 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,023 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,412 views
0 votes
1 answer

How do I get the number of elements in a list

The len() function can be used with several different ...READ MORE

answered Feb 15, 2022 in Python by CoolCoder
• 4,400 points
361 views
0 votes
1 answer

Reverse a string in Python

>>> 'hello world'[::-1] 'dlrow olleh' This is extended slice syntax. It ...READ MORE

answered Feb 15, 2022 in Python by CoolCoder
• 4,400 points
577 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