FAQ: Code Challenge: Dictionaries - Frequency Count

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

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

Learn Python 3

FAQs on the exercise Frequency Count

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!

10 posts were split to a new topic: Solution Sharing

Hey all - I’m aware that the solution solves this differently.

Below is my code. To what seems 100% logical and correct to me, Python runs this and while I get the ‘keys’ correct, my values always come out to be incorrect - where am I going wrong?

def frequency_dictionary(words):
    counter = 0 #Initialize counter
    dictionary = {} #Initialize dictionary
    words_length = len(words) #Obtain length of the list that user inputs
    for word in words:
        for i in range(0, words_length): #Iterate through user inputted list
            if word == words[i]: #See if matching at all
                counter += 1 #If matching, add +1 to counter
        dictionary.update({word: counter}) #Update dictionary
    return dictionary

When I test my code with:

print(frequency_dictionary(["apple", "apple", "cat", 1]))
# should print {"apple":2, "cat":1, 1:1}

I get an output of: {‘apple’: 4, ‘cat’: 5, 1: 6}

Thank you in advance. Identifying the problem will show me what area I lack knowledge in.

Hi @byte1992117061,

You have a single integer variable, counter, that is intended to keep track of the frequency of all the different words. Since it is a single value, it cannot be used to track all of them simultaneously.

You can simplify your code, as follows.

  • Eliminate the variable counter, and just use dictionary to keep a count of the frequencies. It should start out empty, and will ultimately contain one item for each different value in words.
  • Iterate through all the items in words. If the item is already represented in dictionary, add 1 to the corresponding value, otherwise add the item to dictionary as a new key with an associated value of 1.
1 Like

Declare and reset counter inside the outer loop.

1 Like

Perfect. Thanks again Roy - works like a charm.

1 Like

Thanks appylpye! Appreciate your detailed response.

1 Like

Thanks, @byte1992117061 and @mtf

Be aware of an efficiency issue.

Let’s consider n to represent the number of items in words. With this solution, the outer loop executes n times. During each of those iterations, the inner loop executes n times. Accordingly, the inner loop executes a total of n * n times during the entire solution. For four items, that’s not a real problem, however for a large number of items, say 1,000, we would have a total of 1,000,000 iterations of the inner loop. With a more efficient solution, involving a single loop, we could have this down to 1,000 loop iterations, which is n, or one iteration per item in words.

Edited on April 25, 2020, to add some detail to the discussion

3 Likes

Noted and applied to my code - done and done! Thanks for breaking down the n * n part. It clears up what I previously thought I had understood .

1 Like

If we assign a 0 value on the 1st iteration, as per the Codecademy solution, I would expect the frequencies to all be underestimated by 1. That is not the case, it works but I don’t quite get how/why?

Consider,

>>> def frequency(u):
	s = u.split()
	v = {w: 0 for w in set(s)}  # unique words initialized to 0
	for w in s:
		v[w] += 1
	return v

>>> frequency('The sky is blue, the sky is big, the sky is high')
{'The': 1, 'sky': 3, 'is': 3, 'blue,': 1, 'the': 2, 'big,': 1, 'high': 1}
>>> 

Or, better still, a sorted frequency table in descending order…

>>> def frequency(u):
	s = u.split()
	v = {w: 0 for w in set(s)}
	for w in s:
		v[w] += 1
	return dict(sorted(v.items(), key=lambda x: x[1], reverse=True))

>>> frequency('The sky is blue, the sky is big, the sky is high')
{'sky': 3, 'is': 3, 'the': 2, 'blue,': 1, 'big,': 1, 'The': 1, 'high': 1}
>>> 

I like the different approaches to solving this problem.
Please see my code below. It works fine. I just want to share with you another way of approaching the challenge.
def frequency_dictionary(words):
new_dictionary={}
for word in words:
new_dictionary[word]=words.count(word)
return new_dictionary

1 Like
def frequency_dictionary(words):
  dic = {}
  for elem in words:
    dic.update({elem:(counter(words, elem))})
  return dic

def counter(list, word):
  count = 0
  for elem in list:
    if elem == word:
      count += 1
  return count 

I am still struggling with this same question. I don’t get why if it is not in the dictionary we add it to the dictionary with a value of 0 instead of 1.