user8990725
user8990725

Reputation:

Create a new list with changes of value of a previous list

I have a list with floats, for example

numbers = [1000.45,1002.98,1005.099,1007.987]

I want to create a new list containing the deltas of the value in numbers - i.e. I want the difference between numbers[0],[numbers[1] to be the first value in a new list. So the first number in my new list should be 2.53, the second should be 2.119, and so forth.

I tried list comprehension

newnumbers= [i[-1] - i[-2] for i in numbers]

but it gives a TypeError

TypeError: 'float' object is not subscriptable

I tried converting the list to integers, but then it gives the same type of error.

How can I create my desired list?

Upvotes: 0

Views: 159

Answers (7)

Sunitha
Sunitha

Reputation: 12015

>>> numbers = [1000.45,1002.98,1005.099,1007.987]
>>> [round((y-x),2) for x,y in zip(numbers, numbers[1:])]
[2.53, 2.12, 2.89]

Upvotes: 0

Nick
Nick

Reputation: 3845

Here is a method that doesn't require importing any modules:

numbers = [1000.45,1002.98,1005.099,1007.987]
sliced = numbers[1:]

answer = list(map((lambda x,y: y - x),sliced,numbers))

This gives:

[-2.5299999999999727, -2.119000000000028, -2.88799999999992]

You can then do:

final_answer = [round(x,2) for x in answer]

which gives:

[-2.53, -2.12, -2.89]

Upvotes: 0

Sathish G
Sathish G

Reputation: 153

newnumbers  = []
for i in range(1,len(numbers)-1):
    newnumbers.append(numbers[i]-numbers[i-1])  

Upvotes: 1

liamvharris
liamvharris

Reputation: 360

You've got the right idea, but just haven't quite got the syntax right. You should use:

newnumbers = [(numbers[i] - numbers[i-1]) for i in range(1, len(numbers))]

In your version you're trying to index a number, but you need to index your list instead.

Upvotes: 1

dennlinger
dennlinger

Reputation: 11440

Note how you access your elements directly, instead of using list indices; The correct way to do the latter in Python would be

for index in range(len(numbers)):

What you are using is essentially the numbers themselves. Also, note that you would have to exclude the first index of your list, since you only want the differences (otherwise, the first call would look at the last index again, according to Python's behavior of calling mylist[-1]), which is why you can restrict your range to range(1,len(numbers)). In the form of a list comprehension, it would now work like this:

newnumbers = [numbers[i] - numbers[i-1] for i in range(1,len(numbers))]

Upvotes: 0

Hearner
Hearner

Reputation: 2729

for i in numbers i is equal to 1000.45 in the first loop, then 1002.98 etc. So i[-1] = 1000.45[-1] which means nothing, you cannot subscriptable a float

numbers = [1000.45,1002.98,1005.099,1007.987]
newnumbers= [numbers[i+1]-numbers[i] for i in range(len(numbers)-1)]
print(newnumbers)
#[2.5299999999999727, 2.119000000000028, 2.88799999999992]

If you want 2 decimal points

newnumbers= [float("%0.2f"%(numbers[i+1]-numbers[i])) for i in range(len(numbers)-1)]
#[2.53, 2.12, 2.89]

Upvotes: 0

andrew_reece
andrew_reece

Reputation: 21264

It's easy with Pandas, use diff():

import pandas as pd

pd.Series(numbers).diff()

0      NaN
1    2.530
2    2.119
3    2.888
dtype: float64

Upvotes: 2

Related Questions