Python – Time Strings to Seconds in Tuple List
Given Minutes Strings, convert to total seconds in tuple list.
Input : test_list = [(“5:12”, “9:45”), (“12:34”, ), (“10:40”, )]
Output : [(312, 585), (754, ), (640, )]
Explanation : 5 * 60 + 12 = 312 for 5:12.
Input : test_list = [(“5:12”, “9:45”)]
Output : [(312, 585)]
Explanation : 5 * 60 + 12 = 312 for 5:12.
Method : Using loop + split()
In this, we separate the minute and second components using split() and perform mathematical computation to convert the value to required seconds, strings converted to integers using int().
Python3
# Python3 code to demonstrate working of# Time Strings to Seconds in Tuple List# Using loop + split()# initializing listtest_list = [("5:12", "9:45"), ("12:34", "4:50"), ("10:40", )]# printing original listprint("The original list is : " + str(test_list))res = []for sub in test_list: tup = tuple() # iterating each tuple for ele in sub: # perform conversion min, sec = ele.split(":") secs = 60 * int(min) + int(sec) tup += (secs, ) res.append(tup)# printing resultprint("The corresponding seconds : " + str(res)) |
Output
The original list is : [('5:12', '9:45'), ('12:34', '4:50'), ('10:40', )]
The corresponding seconds : [(312, 585), (754, 290), (640, )]Time Complexity: O(n2)
Auxiliary Space: O(n)


