FAQ: Code Challenge: Dictionaries - Count First Letter

This community-built FAQ covers the “Count First Letter” exercise from the lesson “Code Challenge: Dictionaries”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise Count First Letter

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

13 posts were split to a new topic: Is it possible to solve this with list comprehension?

6 posts were split to a new topic: What does key[0] from the dictionary represent?

5 posts were split to a new topic: How does accessing the first letter work?

A post was merged into an existing topic: Any advice for a newbie who feels like maybe I should just quit?

A post was split to a new topic: Solution Sharing

3 posts were split to a new topic: I don’t understand the question?

2 posts were split to a new topic: Any advice for a newbie who feels like maybe I should just quit?

My solution:

def count_first_letter(names):

d = {}

c = 0

for i,j in names.items():

a = i[0]

for s in a:

  if a in d:

    c+= len(j)


    c = len(j)

d[a] = c

return d

My simple solution:

def count_first_letter(names):
  for i in names.keys():
    if i[0] in a.keys():
  return a

looking at this makes me feel like an idiot, thanks.

def count_first_letter (names):

result = {}

for key,value in names.items():


return result

Thank you all for sharing your solutions! I found this challenge very difficult and approached it in a too complicated way (e. g. tried to loop through the whole dictionary and not just through the keys) and it helps a lot to see how other people approached it!


Just found this different way to do this exercise, different from given solution I mean if anyone is interested.
def count_first_letter(names):
new_dict = {}
for key, value in names.items():
if key[0:1] not in new_dict:
new_dict[key[0:1]] = len(value)
new_dict[key[0:1]] = new_dict[key[0:1]] + len(value)
return new_dict

Here is my way too complex solution:

def count_first_letter(names):
  ind = []
  for key, value in names.items():
    length = len(value)
    while length > 0:
      length -= 1
  return count_values(ind)

def count_values(list):
  new_list = []
  count = 1
  ind = []
  for elem in list:
    if elem not in ind:
  return convert_to_dic(new_list)

def convert_to_dic(list):
  dic = {}
  for i in range(0, (len(list)-1), 2):
  return dic

After redoing this after a year I came up with this solution based on something I read somewhere on codecademy but I can’t remember where on defaultdict !

from collections import defaultdict

names = {"Stark": ["Ned", "Robb", "Sansa"], "Snow" : ["Jon"], "Lannister": ["Jaime", "Cersei", "Tywin"]}

def count_first_letter(names):

    letters = defaultdict(int)

    for key in names:

        letters[key[0]] += len(names[key])

    return letters

My code is similar but giving error

def count_first_letter(names):
  letter_count = {}
  for key, value in names.items():
    if key[0] in letter_count.keys():
      letter_count[key] += len(value) 
      letter_count[key[0]] = len(value)
  return letter_count

I’ve also used an else statement but it worked with the “not in” in boolean operator

def count_first_letter(names):
  initials_count = {}
  for key in names:
    if key[0] not in initials_count:
      initials_count[key[0]] = len(names[key])
      initials_count[key[0]] += len(names[key])
  return initials_count 

My solution:

def count_first_letter(names):

  result = {}
  key_letters = [key[0] for key in names.keys()]
  values_letters = [len(value) for value in names.values()]
  for index, key in enumerate(key_letters):
    if key not in result.keys():
      result[key] += values_letters[index]

  return result