For type consistency, I recommend using an empty dictionary {}
instead of ' ' for empty days. With that in mind, suppose we have the following:
sched = {'Mo': {'10.00': 'COMP100'},
'Tu': {},
'We': {'12.00': 'UNIV199'},
'Th': {},
'Fr': {}}
# code = "COMP100"
code = "ECON201"
Then, we could search for the code as follows:
found = False
for d in sched.values():
for s in d.values():
if s == code:
found = True
break
if found:
break
if found:
print('code found in schedule')
else:
print('code not found in schedule')
Alternatively, you can avoid dealing with break
s and storing booleans if you define a function. For instance,
def is_in(sch,c):
for d in sch.values():
for s in d.values():
if s == c:
return True
return False
if is_in(sched,code):
print('code found in schedule')
else:
print('code not found in schedule')
Alternatively, here's a recursive version of the function that works for arbitrarily many layers of nesting.
def is_in(sch,c):
for v in sch.values():
if isinstance(v,dict):
if is_in(v,c):
return True
elif v == c:
return True
return False
if is_in(sched,code):
print('code found in schedule')
else:
print('code not found in schedule')
On the other hand, from your image, it seems that you're looking for a way to remove a course from a schedule. To that end, consider the following.
sched = {'Mo': {'10.00': 'COMP100'},
'Tu': {},
'We': {'12.00': 'UNIV199'},
'Th': {},
'Fr': {}}
# code = "COMP100"
code = "ECON201"
# print(sched)
def rem(c,sch):
for k in sch.copy():
if isinstance(sch[k],dict):
rem(c,sch[k])
else:
if sch[k]==c:
del sch[k]
rem(code,sched)
print(sched)
In the case that the course does not appear, nothing happens. If the course is found, then all instances of that course are deleted.