Generation

generate functionThu, 15 Dec 2022

Create a function that allows you to move values between directories keys with nested lists inside. It should check for: 1) if the specified key is in the dictionary and show ERROR NO SUCH KEY if it does not exist 2) if the value is present in the dictionary and show ERROR NO SUCH VALUE if it doesn't exist 3) if both the key and the value are in the dictionary, the function should move the value v to another k and return a new dictionary. Note that dictionary values are lists! Example: Input: directories = { '1': ['2207 876234', '11-2'], '2': ['10006'], '3': [] } v = '11-2' k = '3' Output: directories = { '1': ['2207 876234'], '2': ['10006'], '3': ['11-2'] }

def move(directories, str1, str2): # write your code here dic = directories if str1 not in dic: return 'ERROR NO SUCH KEY' if str2 not in dic: dic[str2] = [] if str1 in dic: if str2 not in dic[str1]: return 'ERROR NO SUCH VALUE' dic[str2].append(str2) for i in range(len(dic[str1])): if str2 in dic[str1][i]: dic[str1].remove(str2) return dic print(move(directories, '11-2', '3'))

Questions about programming?Chat with your personal AI assistant