Skip to content

Instantly share code, notes, and snippets.

@joan0fsnark
Created September 6, 2021 21:06
Show Gist options
  • Save joan0fsnark/ac04566f8f609ab86c501c99880d6058 to your computer and use it in GitHub Desktop.
Save joan0fsnark/ac04566f8f609ab86c501c99880d6058 to your computer and use it in GitHub Desktop.
Function takes a dictionary called 'names' as a parameter; 'names' should be a dictionary where the key is a last name and the value is a list of first names. Returns a new dictionary where each key is the first letter of a last name, and the value is the number of people whose last name begins with that letter.
def count_first_letter(names):
letters = {}
for key in names:
first_letter = key[0]
if first_letter not in letters:
letters[first_letter] = 0
letters[first_letter] += len(names[key])
return letters
# Uncomment these function calls to test your function:
print(count_first_letter({"Stark": ["Ned", "Robb", "Sansa"], "Snow" : ["Jon"], "Lannister": ["Jaime", "Cersei", "Tywin"]}))
# should print {"S": 4, "L": 3}
print(count_first_letter({"Stark": ["Ned", "Robb", "Sansa"], "Snow" : ["Jon"], "Sannister": ["Jaime", "Cersei", "Tywin"]}))
# should print {"S": 7}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment