FAQ: Collections - Counter

This community-built FAQ covers the “Counter” exercise from the lesson “Collections”.

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

Learn Intermediate Python 3

FAQs on the exercise Counter

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

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!

I went to the last exercise and it didn’t pass. Here is the code:

opening_inventory = ['shoes', 'shoes', 'skirt', 'jeans', 'blouse', 'shoes', 't-shirt', 'dress', 'jeans', 'blouse', 'skirt', 'skirt', 'shorts', 'jeans', 'dress', 't-shirt', 'dress', 'blouse', 't-shirt', 'dress', 'dress', 'dress', 'jeans', 'dress', 'blouse']

closing_inventory = ['shoes', 'skirt', 'jeans', 'blouse', 'dress', 'skirt', 'shorts', 'jeans', 'dress', 'dress', 'jeans', 'dress', 'blouse']

# Write your code below!
from collections import Counter

def find_amount_sold(opening, closing, item):
  opening_count = Counter(opening)
  closing_count = Counter(closing)
  opening_count.subtract(closing_count)
  return opening_count[item]

tshirts_sold = find_amount_sold(opening_inventory, closing_inventory, "t-shirt")
print(tshirts_sold)

I knew that there was nothing wrong. I tried to switch the "t-shirt" with 't-shirt' and it passed. How did Codecademy check that I used single quotes? It must be because it didn’t let me pass with double quotes. It might not be checking for equality because in Python "t-shirt" and 't-shirt' are equal. How do you check if a string is in single quotes and double quotes? Do most Python Learning Platforms suggest using double quotes or single quotes? Maybe I should get used to using single quotes also because I’m so used to double quotes.

Python translates double quotes to single quotes. It’s not an issue which we prefer to use. The occasional SCT might be matching literal expressions and not have a pattern match for the variations.

("|'|'''|""")

Such is the case in an environment with hundreds of authors and virtually nothing for a style guide. They write according to their individual styles and comprehension level. That leads to the diverse lesson checks that are the one irritant most common. It’s a MOOC, warts and all. Make the most of it.

Normally this doesn’t happen, the exercises don’t care if you use single quotes or double quotes.

What do those acronyms mean?

Submission Correctness Test

Massive Open Online Course

The latter should be followed up for accuracy. It might be ‘online open’.

I understand MOOC which means that the course in a course that is open to many people and is online which means that a lot of people with different abilities contribute to this course. But I don’t quite understand the S in SCT.

When we click Save or Run in an exercise (in the LE) it invokes the Submission Correctness Test. Our code in the editor is the submission to be tested.

Sometimes I view the test files of exercises. Like in this exercise which is the last one of the lesson: https://www.codecademy.com/courses/learn-intermediate-python-3/lessons/collections-python/exercises/review-of-specialized-containers-python.
The _test.py file:

load_file_in_context("clothes_app.py")

try:
  if len(promoted_bundles) != 2:
    fail_tests("`promoted_bundles` did not have the correct number of elements. Expected 2 but got " + str(len(promoted_bundles)))

except NameError:
  fail_tests("`Did you create a `list` called `promoted_bundles`?")
  
try:
  if round(promoted_bundles[0].bundle_price, 2) != 106.95:
    fail_tests("`promoted_bundles` did not contain the correct data. Make sure that the sum of each bundle is calculated correctly and that only bundles with a price of over 100 dollars are included.")
  if round(promoted_bundles[1].bundle_price, 2) != 103.95:
    fail_tests("`promoted_bundles` did not contain the correct data. Make sure that the sum of each bundle is calculated correctly and that only bundles with a price of over 100 dollars are included.")


except NameError:
  fail_tests("Did you create a `list` called `promoted_bundles`?")
    
pass_tests()

Where is load_file_in_context(), pass_tests() and fail_tests() defined?

Is the _test.py file a protected file like in a encapsulation exercise in the course? It taught me that to signal to other people that an attribute is protected you use one _ before the attribute.

An underscore in the name implies that the attribute is only ever accessed from a method.

What attribute? :thinking: :thinking: :thinking:

The one with the underscore in the name.

That’s the _test.py file, that’s not an attribute in the the _test.py file.

So the lesson allowed single quotes instead of double quotes thus Python didn’t recognize the difference between single quotes and double?

These last exercises are a nightmare for learners. The directions are unclear and badly written.

opening_inventory = ['shoes', 'shoes', 'skirt', 'jeans', 'blouse', 'shoes', 't-shirt', 'dress', 'jeans', 'blouse', 'skirt', 'skirt', 'shorts', 'jeans', 'dress', 't-shirt', 'dress', 'blouse', 't-shirt', 'dress', 'dress', 'dress', 'jeans', 'dress', 'blouse']

closing_inventory = ['shoes', 'skirt', 'jeans', 'blouse', 'dress', 'skirt', 'shorts', 'jeans', 'dress', 'dress', 'jeans', 'dress', 'blouse']

# Write your code below!
from collections import Counter

def find_amount_sold(opening, closing, item):
  opening_count = Counter(opening)
  closing_count = Counter(closing)
  opening_count.subtract(closing_count)
  return opening_count[item]

tshirts_sold = find_amount_sold(opening_inventory, closing_inventory, "t-shirt")
print(tshirts_sold)

Checkpoiint 4 Instructions

Awesome! Now we have our Counter object with the difference in item inventory. You may have noticed earlier we defined a parameter named item in our function declaration. This is because the goal of our function is to return the difference in inventory for a specific item rather than all of them!

Using the parameter item, access the item’s inventory from the opening_count Counter object and return it.

Checkpoint 4 Hint

Don’t forget to return only the result of the subtraction for the specific item . This will look like: opening_count[item] .

My Question

Something is not making sense to me – from the Hint it seems that the function’s goal is to return the difference between opening_count and closing_count (both of which are Count objects) for a specific item. But the correct answer is just returning the value for opening_count(item).

I tried inserting a print command into the function and found that the opening_count.subtract(closing_count) just returns None.

Please help me understand. Thanks!