The following script is quite forgiving in terms of input, so I don't see the path around a couple of variables.
I added another test case for Sunday with 3 times a week and several different JUNKs.
import pprint input_array = [ 'JUNK', 'Mon', 'JUNK', '10am', 'JUNK', '-', ' 5pm', '6pm', '-', '9pm', 'JUNK', 'Tue', '10am', '-', 'JUNK', '5pm', "Sun ", " 8am", "- ", "JUNK", "MORE JUNK", " 9pm", "10am ", "-", " 1pm", " 6pm", "-", "8pm"] days_of_the_week = {'mon' : 'monday', 'tue' : 'tuesday', 'wed' : 'wednesday', 'thu' : 'thursday', 'fri' : 'friday', 'sat' : 'saturday', 'sun' : 'sunday'} business_hours = [] start = None for entry in input_array: entry = entry.strip().lower() if entry in days_of_the_week: current_day = days_of_the_week[entry] elif entry[0].isdigit() and start: business_hours.append({"weekday_name": current_day, "starting_time": start, "ending_time": entry}) start = None elif entry[0].isdigit(): start = entry pprint.pprint(business_hours)
The output of the following result:
[{'ending_time': '5pm', 'starting_time': '10am', 'weekday_name': 'monday'}, {'ending_time': '9pm', 'starting_time': '6pm', 'weekday_name': 'monday'}, {'ending_time': '5pm', 'starting_time': '10am', 'weekday_name': 'tuesday'}, {'ending_time': '9pm', 'starting_time': '8am', 'weekday_name': 'sunday'}, {'ending_time': '1pm', 'starting_time': '10am', 'weekday_name': 'sunday'}, {'ending_time': '8pm', 'starting_time': '6pm', 'weekday_name': 'sunday'}]