-1

With the help of this answer, I can calculate the average of a list of timedeltas in Python.

timedeltas = [ ... ]

# giving datetime.timedelta(0) as the start value makes sum work on tds 
average_timedelta = sum(timedeltas, datetime.timedelta(0)) / len(timedeltas)

Is there a solution for obtaining the median of timedeltas in a list?

Soroosh Sorkhani
  • 64
  • 1
  • 2
  • 13
  • 1
    You could sort the list, then pick the middle value, same way you calculate the median of any other sort of array – macaw_9227 Jan 08 '20 at 21:43

1 Answers1

1

Use statistics.median()

As an example:

import statistics

timedeltas = [1,2,3,4,5,6]
print(statistics.median(timedeltas))

importing statistics module, you can also use mean() (average value):

print(statistics.mean(timedeltas))
Kalma
  • 585
  • 2
  • 13