Each Answer to this Q is separated by one/two green lines.
string in this format
"HH:MM", for example
"03:55", that represents 3 hours and 55 minutes.
I want to convert it to
datetime.time object for easier manipulation. What would be the easiest way to do that?
>>> datetime.time(*map(int, '03:55'.split(':'))) datetime.time(3, 55)
It is perhaps less clear to future readers, but the
*map method is more than 10 times faster. See below and make an informed decision in your code. If calling this check many times and speed matters, go with the generator (“map”).
In : timeit(datetime.strptime('15:00', '%H:%M').time()) 7.76 µs ± 111 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) In : timeit(dtime(*map(int, SHUTDOWN_AT.split(':')))) 696 ns ± 11.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)