Create a function called lists2Dictionary. It should take 2 lists as arguments.
ID: 3860788 • Letter: C
Question
Create a function called lists2Dictionary. It should take 2 lists as arguments. The function should return a dictionary where the keys are the items in list1 and the values are the items in list2. Complete unit testing on the function. Example:
list1 = ['1','2','3','4']
list2 = ['v1','v2','v3','v4']
d = lists2Dictionary(list1,list2)
print d
Your function should consider the following unit tests (Include these after your function definition):
The lists are different sizes
List1 has a duplicate value
Blank lists
Lists of ints, floats, strings and booleans
*Include documentation in your function definition file which explains the function and behavior of at least the 4 unit tests above. Example : If the the lists are different sizes the dictionary will contain only the number of key/value pairs of the shorter list.
Explanation / Answer
Below is the program with the test cases and i have written documentation in the each test case
def lists2Dictionary(list1,list2):
dict1 = {}
size = min(len(list1),len(list2))
for i in range(0,size):
key = list1[i]
value = list2[i]
dict1[key] = value
return dict1
# Test Case 1 lists are of same size
# Explanation basic list
list1 = ['1','2','3','4']
list2 = ['v1','v2','v3','v4']
d = lists2Dictionary(list1,list2)
print (d)
# Test Case 2 Lists are of differnt size
# Explanation : here we will consider the smaller size list values only
list1 = ['1','2','3','4','5']
list2 = ['v1','v2','v3','v4']
d = lists2Dictionary(list1,list2)
print (d)
# Test Case 3 Lists are empty
# Explanation : here it will return empty dictionary
list1 = []
list2 = []
d = lists2Dictionary(list1,list2)
print (d)
# Test Case 4 list had duplicate values
# Explanation : here in case of duplicate keys last value will be overridden so answer will be {'1': 'v3', '2': 'v2'}
list1 = ['1','2','1']
list2 = ['v1','v2','v3']
d = lists2Dictionary(list1,list2)
print (d)
# Test Case 5 list boolean string , integers, float
# Explanation : here dictionary will hold multiple types values together
# Python is awesome
list1 = [12,2.3,'1',True]
list2 = ['v1','v2','v3',False]
d = lists2Dictionary(list1,list2)
print (d)
# Final output
{'1': 'v1', '2': 'v2', '3': 'v3', '4': 'v4'}
{'1': 'v1', '2': 'v2', '3': 'v3', '4': 'v4'}
{}
{'1': 'v3', '2': 'v2'}
{12: 'v1', 2.3: 'v2', '1': 'v3', True: False}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.