The Wayback Machine - https://web.archive.org/web/20220929041538/https://www.geeksforgeeks.org/python-time-strings-to-seconds-in-tuple-list/
Skip to content
Related Articles

Related Articles

Python – Time Strings to Seconds in Tuple List

View Discussion
Improve Article
Save Article
  • Last Updated : 23 Aug, 2022
View Discussion
Improve Article
Save Article

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 list
test_list = [("5:12", "9:45"), ("12:34", "4:50"), ("10:40", )]
 
# printing original list
print("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 result
print("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)

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!