Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to seperate list of hours:minutes:seconds in python?

Hello i would like to seperate a list that looks like this:

04:05:43.0
04:05:44.0
04:05:45.0
04:05:46.0
04:05:47.0
04:05:48.0
04:05:49.0
04:05:50.0
04:05:51.0
04:05:52.0
04:05:53.0
04:05:54.0
04:05:55.0
04:05:56.0
04:05:57.0
04:05:58.0
04:05:59.0
04:06:00.0
04:06:01.0
04:06:02.0
04:06:03.0
04:06:08.0

to 3 seperated lists with hours/minutes and seconds

Expected result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

hrs = [4, 4, 4, 4, 4, 4, ......]
mins = [5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,.... ]
secs = [1, 2, 3, 4, 5, 6,.....]

>Solution :

This code does exactly what you want!

list_ = [
    '04:05:43.0',
    '04:05:44.0',
    '04:05:45.0'
]

# create 3 different lists, seperating hrs mins and secs
hrs = []
mins = []
secs = []

# loop through the list and split the strings into 3 different lists
for i in list_:
    hrs.append(int(i.split(':')[0]))
    mins.append(int(i.split(':')[1]))
    secs.append(int(i.split(':')[2].split('.')[0]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading