An alternative is to turn the time lists into set
's and subtract them. So what’s left are the available times:
schedules_dict = {1: ['00:00'], 2: ['00:30', '00:45'], 3: ['00:00', '00:30', '00:45']}
appointments_dict = {1: ['00:00'], 2: ['00:30'], 3: ['00:00', '00:30']}
output = {}
for indice, disponiveis in schedules_dict.items():
if indice in appointments_dict:
# vê os horários que sobraram
sobrou = list(set(disponiveis) - set(appointments_dict[indice]))
if len(sobrou) > 0:
output[indice] = sobrou
else:
output[indice] = disponiveis
print(output) # {2: ['00:45'], 3: ['00:45']}
One detail is that set
does not guarantee the order of the elements. If you want the times to be in order, just change to use sorted
:
sobrou = sorted(set(disponiveis) - set(appointments_dict[indice]))
At first, compare strings containing digits does not always work as expected, but in this case the times are in HH:MM format (always with 2 digits, according to the ISO 8601 standard), then the ordering is done correctly.