I was coding an appointment app the other day. It is a part of a calendar program I've been working off and on for a year. I used a drop-down list of times in a PyQt6 combo box (QComboBox). My initial list is not too long, but quite long enough.

times = ( 
    "All Day", 
    "5:00 AM", 
    "5:30 AM", 
    "6:00 AM", 
    "6:30 AM", 
    "7:00 AM", 
    "7:30 AM", 
    "8:00 AM", 
    "8:30 AM", 
    "9:00 AM", 
    "9:30 AM", 
    "10:00 AM", 
    "10:30 AM", 
    "11:00 AM", 
    "11:30 AM", 
    "Noon", 
    "12:30 PM", 
    "1:00 PM", 
    "1:30 PM", 
    "2:00 PM", 
    "2:30 PM", 
    "3:00 PM", 
    "3:30 PM", 
    "4:00 PM", 
    "4:30 PM", 
    "5:00 PM", 
    "5:30 PM", 
    "6:00 PM", 
    "6:30 PM", 
    "7:00 PM", 
    )

As I've become more knowledgeable, I wanted to see if I could do something different (shorter). This is what I came up with:

hours = ['All Day'] 
times = range(5, 19) 
for time in times: 
    time = str(time) 
    if int(time) < 12: 
        hours.append(time + ':00 AM') 
        hours.append(time + ':30 AM') 
    else: 
        hours.append(time + ':00 PM') 
        hours.append(time + ':30 PM')

If you book appointments on a 15-minute basis, then the code would look like this:

hours = ['All Day'] 
times = range(5, 19) 
for time in times: 
    time = str(time) 
    if int(time) < 12: 
        hours.append(time + ':00 AM') 
        hours.append(time + ':15 AM') 
        hours.append(time + ':30 AM') 
        hours.append(time + ':45 AM') 
    else: 
        hours.append(time + ':00 PM') 
        hours.append(time + ':15 PM') 
        hours.append(time + ':30 PM') 
        hours.append(time + ':45 PM')

The range parameters can be changed to reflect your hours. 'All Day" will be the first choice at the top of the list. There are two differences in this list from my original. I replaced 'Noon' with ཈ PM' and switched to a 24-hour format. Well, I didn't replace the values. My code did.

But, then I decided I did not want my list in 24-hour format and I wanted 'Noon' back. I came up with this:

hours = ['All Day']
times = range(5, 19)
count = 0
for time in times:
    time = str(time)
    if int(time) < 12:
        hours.append(time + ':00 AM')
        hours.append(time + ':30 AM')
    elif int(time) == 12:
        hours.append('Noon')
        hours.append(time + ':30 PM')
    else:
        count += 1
        num = str(count)
        hours.append(num + ':00 PM')
        hours.append(num + ':30 PM')

The elif statement assigns the suffix 'PM' to the string ཈' and will not work if you are using a 24-hour appointment system. 12 AM would read ཈ PM'. Count converts 24-hour format time to 12-hour format time. Our 15 minute times list would be changed to:

hours = ['All Day']
times = range(5, 19)
count = 0
for time in times:
    time = str(time)
    if int(time) < 12:
        hours.append(time + ':00 AM')
        hours.append(time + ':15 AM')
        hours.append(time + ':30 AM')
        hours.append(time + ':45 AM')
    elif int(time) == 12:
        hours.append('Noon')
        hours.append(time + ':15 PM')
        hours.append(time + ':30 PM')
        hours.append(time + ':45 PM')
    else:
        count += 1
        num = str(count)
        hours.append(num + ':00 PM')
        hours.append(num + ':15 PM')
        hours.append(num + ':30 PM')
        hours.append(num + ':45 PM')

But I still wasn't happy, so I tried rewriting it yet again.

def times_list( time: int, hours: list, suffix: list )->list:
    """
    Converts 24-hour clock format to 12 
    hour and appends the suffix (AM/PM)
    
    :param time: range of numbers
    :param hours: list of times
    :param suffix: AM or PM
    """
    count = 0
    for fix in suffix:
        if count < 2:
            hours.append( f'{time}{fix}' )
            count += 1
    return hours


hours = ["All Day"]
times = range( 5, 19 )
suffix_am = ':00 AM', ':30 AM'
suffix_pm = ':00 PM', ':30 PM'
count = 0

for time in times:
    time = str( time )
    if int( time ) < 12:
        times_list( time, hours, suffix_am )
    elif int( time ) == 12:
        hours.append( "Noon" )
        times_list( time, hours, suffix_pm[1:] )
    else:
        count += 1
        num = str( count )
        times_list( num, hours, suffix_pm )

Again, for 15-minute increments, modify suffix_am and suffix_pm and change line 4 of the function to if count < 4:

I hope you enjoyed this article. You can find more like this on my website. While there, please visit our advertiser's page. I make a small commission on any purchase. That money is used to run and maintain the site. Cheers