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 2,034 views

## 1 answer to this question.

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.

• 4,360 points

## Sum a list of numbers in Python

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

+1 vote

## Count the frequency of an item in a python list

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

## 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

## Sorting a list of strings in Python

Use the sort function mylist.sort() READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

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

## Lowercase in Python

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