William M. answered 01/05/23
Computer/HS Instructor w/7+ years teaching Computer Science
# Note: No const in Python
whereIsWaldo = [["Picard", "Riker"], "Data",["Beverly", "Guinan", "Q", "Geordi"],["Deanna", ["Worf", "Wesley"]]];
def slice_demo():
modified = [whereIsWaldo[0]] # eliminate 'Data' == copy all elements except 'Data'
modified.extend(whereIsWaldo[2:])
modified[1] = [el.replace('Q', 'No One') for el in modified[1]] # change 'Q' to 'No One'
wesleys = [x for x in modified[2][1] if x == 'Wesley'] # make a list of all the Wesleys (just one)
wesley = wesleys[0] # grab the one and only Wesley
print(f'wesley is: {wesley}') # and log it to the console
def main():
slice_demo()
if __name__ == '__main__':
main()
#============================================
# OUTPUT
#
# wesley is: Wesley
#============================================