Snack Count with dict.get()
A school snack booth closes for the afternoon and ends up with one long list of sold items. Some snack names appear once, others repeat several times, and the staff now wants a quick summary showing how many of each item were sold.
Your task is to take the list of snack names and return a dictionary where each key is a snack and each value is the number of times that snack appears. The counting should preserve exact names, so "chips" and "Cookies" would be treated as different strings if they appear that way in the input.
For example, if the orders are ["chips", "juice", "chips", "cookie"], the result should be {"chips": 2, "juice": 1, "cookie": 1}. If the list is empty, the answer should be an empty dictionary because there is nothing to count.
This is a classic frequency-counting task. The input is a list, the output is a dictionary summary, and every time a snack appears again, its count should increase by one.
Example Input & Output
One repeated snack produces one key with a larger count.
Repeated snacks increase their stored counts in the dictionary.
No orders means there is nothing to count.
Algorithm Flow

Solution Approach
Problems like this are a good fit for a dictionary because each distinct snack name can act as a key, while the stored value keeps track of how many times that snack has appeared so far.
The Python feature that makes this especially neat is dict.get(). When you count frequencies, the annoying part is usually the first appearance of a value. If the snack has not been seen before, there is no existing count yet. get() solves that cleanly by letting you supply a default.
A simple solution is:
That update line does all the important work. If snack is already in the dictionary, counts.get(snack, 0) returns its current count. If it is missing, Python returns the default value 0. Then the code adds one either way.
This makes the loop short without hiding the logic. Each snack is processed once, and the dictionary gradually becomes the final tally. The first time "chips" appears, its count becomes 1. The next time it appears, that value becomes 2, and so on.
You could write the same algorithm with an if statement by checking whether the snack is already present in the dictionary. That works too, but dict.get() keeps the update step compact and easy to read. It is a very common Python pattern for frequency counting, which is why this challenge focuses on it directly.
The overall runtime is O(n) because the list is scanned once, and the extra space depends on how many distinct snack names appear in the final dictionary. For a first dictionary-counting exercise, this is exactly the kind of code that feels natural in Python.
Best Answers
def count_snacks(orders):
counts = {}
for snack in orders:
if snack in counts:
counts[snack] += 1
else:
counts[snack] = 1
return countsComments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
