Python list and datetime module

108 Views Asked by At

I have this list in Python:

youtube_time = ['23:15:02', '23:03:19', '18:52:23',
                '20:26:46', '57:41:07', '51:45:11',
                '47:20:26', '43:24:26', '127:41:49']

Each item in this list is the length of the video in the format: hours, minutes, seconds. How can I add up all the items in the list to get this result: 17 days, 5:30:29

You need to use the datetime module

I`m begginer in Python, and i dont know how

4

There are 4 best solutions below

0
MSpruijt On BEST ANSWER

Just use the timedelta from datetime:

from datetime import timedelta

youtube_time = ['23:15:02', '23:03:19', '18:52:23', '20:26:46', '57:41:07', '51:45:11', '47:20:26', '43:24:26', '127:41:49']


def get_total_time_delta(time_list: list) -> timedelta:
    total_time = timedelta()
    for time in time_list:
        hours, minutes, seconds = [int(i) for i in time.split(':')]
        total_time += timedelta(hours=hours, minutes=minutes, seconds=seconds)
    return total_time

result = get_total_time_delta(youtube_time)

print(result)
2
Ömer Sezer On

Firstly, calculate the sum of seconds. Then calculate days, minutes, and seconds.

Three lines of Code:

from datetime import timedelta
youtube_time = ['23:15:02', '23:03:19', '18:52:23', '20:26:46', '57:41:07', '51:45:11', '47:20:26', '43:24:26', '127:41:49']

sum_seconds = sum(int(x) * 60 ** i for time_str in youtube_time for i, x in enumerate(reversed(time_str.split(':'))))
sum_all = timedelta(seconds=sum_seconds)
print(sum_all)

Output:

17 days, 5:30:29
0
Marco Parola On

You can use datetime python packacge. Run the following code, it should be quite easy to understand:

from datetime import datetime, timedelta

# Your input
youtube_time = ['23:15:02', '23:03:19', '18:52:23',
                '20:26:46', '57:41:07', '51:45:11',
                '47:20:26', '43:24:26', '127:41:49']

# Initialize a timedelta object to store the total time
total_time = timedelta()


for video_time in youtube_time:
    # Split the time string into hours, minutes, and seconds
    h, m, s = map(int, video_time.split(':'))

    # Create a timedelta object for the current video duration
    video_duration = timedelta(hours=h, minutes=m, seconds=s)
    
    # sum
    total_time += video_duration

# Calculate the total days and remaining hours, minutes, and seconds
total_days = total_time.days
total_seconds = total_time.seconds
total_hours, remainder = divmod(total_seconds, 3600)
total_minutes, total_seconds = divmod(remainder, 60)

print(f'Total time: {total_days} days, {total_hours}:{total_minutes:02d}:{total_seconds:02d}')
0
SIGHUP On

You only need the timedelta for ease of presentation of the result. You do not need it for intermediate calculations.

Also, rather than a complex generator, it's easier (and faster) to write your own function to convert the strings to integer seconds.

from datetime import timedelta

def secs(s):
    total = 0
    m = 3_600
    for v in s.split(":"):
        total += int(v) * m
        m //= 60
    return total

youtube_time = [
    "23:15:02",
    "23:03:19",
    "18:52:23",
    "20:26:46",
    "57:41:07",
    "51:45:11",
    "47:20:26",
    "43:24:26",
    "127:41:49",
]

print(timedelta(seconds=sum(map(secs, youtube_time))))

Output:

17 days, 5:30:29