TooMannames.py Submit Run Grades Reset 1 #irite a function called name counts. n
ID: 3707905 • Letter: T
Question
TooMannames.py Submit Run Grades Reset 1 #irite a function called name counts. name counts will take 2#as input list of full names. Each name will be two words 3 #separated by a space, like "David Joyner 4? 5#The function should return 6#dictionary 7#values should be the number of times that first name 8 appeared Traceback (most recent call las File "TooManyNames-py", line 3 a dictionary. The keys to the 4, in will be the first names from the list, and the print (nare_counts (nare list)) File "Tooany Names py", line 2 return ta dict[names[e11) Conmand exited with non-zero stat 8, in nane_counts 10 #HINT: Use split() to split names into first and last KeyError: 'aren 13#Add your function here! 14 def nane_counts (name1ist) 15 ta dict-t for neme in nane 1ist: names-name.splito if nanes [1 in names 19 ta dict[names[e11+-1 26 return(to-dict[names[0] will test your function 23 #Below are some lines of code that 24#You can change the value of the variable(s) to test your 25#function with different inputs 26 27 #If your function works correctly, this will originally 28#print (although the order of the keys may vary) 29 #{"Shelba': 5, .Maren': 1, 'Nicol. : 1, "David. : 2, .Brenton .: 2) 3 name list ["David Joyner" "David Zuber", "Brenton Joyner, 31 32 "Brenton Zuber "Nicol Barthel", Shelba Barthel", "Shelba Crowley","Shelba Fernald", "Shelba odle", 34 print (name counts (name list)) 37 38Explanation / Answer
def name_counts(name_list): d = {} for name in name_list: first_name = name.split()[0] if first_name not in d: d[first_name] = 0 d[first_name] += 1 return d