I have a nested list with time values. I want to check and replace times that do not in time format "HH:MM". The first step i want to do is adding ":00" for numbers that have not ":" . My list is look like the below list (mylist) .
mylist = [['x', '6 - 9:30 AM - 10:30 AM - 2 PM - 5 PM - 9 PM], ['y', 7:30 AM - 2:30 PM, 7:30 AM - 2:30 PM, 7:30 AM - 1:30 PM']]
res = [['x', '6:00 - 9:30 AM - 10:30 AM - 2:00 PM - 5:00 PM - 9:00 PM], ['y', 7:30 AM - 2:30 PM, 7:30 AM - 2:30 PM, 7:30 AM - 1:30 PM]]
I have tried this code:
for idx, (id,name) in enumerate(mylist):
for n2,j in enumerate(name.split(' - ')) :
if ':' not in j and id not in j:
print(name)
if ":" not in name.split('-')[0] and ":" not in name.split('-')[1]:
list1[idx][n2] = name.split('-')[0].split(' ')[0] + ':00' + ' AM' + ' - ' + \
name.split('-')[1].split(' ')[1].strip() + ':00' + ' PM'
# print(name)
elif ":" not in name.split('-')[0]:
list1[idx][n2] = name.split('-')[0].split(' ')[0] + ':00' + ' AM' + ' - ' + \
name.split('-')[1].split(' ')[1].strip() + ' PM'
elif ":" not in name.split('-')[1]:
list1[idx][n2] = name.split('-')[0].split(' ')[0] + ' AM' + ' - ' + name.split('-')[1].split(' ')[
1].strip() + ':00' + ' PM'
else:
list1[idx][n2] = name.split('-')[0].split(' ')[0] + ' AM' + ' - ' + name.split('-')[1].split(' ')[
1].strip() + ' PM'
but it rised the below error:
name.split('-')[1].split(' ')[1].strip() + ' PM' IndexError: list assignment index out of range
How can i solve the issue?