list comprehension division

Question:

hello I am trying to divide two lists of distance and time differences however sometimes the time difference is 0 I don’t know how to account for this in list comprehension.

velocity = [j / i for i, j in zip(distance[1:], time_diff[:-1]) if time_diff[i] > 0]

I get this error for this implementation.

TypeError: list indices must be integers or slices, not float
Asked By: JaredCusick

||

Answers:

As the comment said, in your if condition, you should add i>0 because for i, j in zip(distance[1:], time_diff[:-1]) gives you the element in your lists, not indices of the list.

velocity = [j / i for i, j in zip(distance[1:], time_diff[:-1]) if i > 0]
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.