Question: Python Question: Write a function called add_list_to_dict that has three parameters: dct , key , and lst . A dictionary, a key, and a list,

Python Question:

Write a function called add_list_to_dict that has three parameters: dct , key , and lst . A dictionary, a key, and a list, respectively.

Given the three parameters, the add_list_to_dict function returns a new dictionary with all of the key-value pairs from the original dictionary, and also sets a new key-value pair according to the given parameters. If anything besides a list is given as the third parameter, lst , the function should return a None-typed object.

my_dct = {'name': 'Smiley'}

my_list = [1, 3]

d = add_list_to_dict(my_dct, 'key', mylist)

print(d['key']) # --> [1, 3]

print(d['name']) # --> 'Smiley'

d2 = add_list_to_dict(my_dct, 'key', "I shouldn't add a string value.")

print(d2) @ --> None

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!