Ship it?

Lists and Dicts

Counting, grouping and merging with lists and dictionaries. AI code can miss a key that isn't there yet, invent a method that doesn't exist, or quietly do a different job.

  1. Read what the AI wrote

  2. Spot the broken line

  3. Ship it, or send it back

Some of the code is fine.

0 of 10 reviewed

1. Count the items

Easy

You asked

Write count_items(items): a dict that maps each item to how many times it appears in the list.

AI assistant

This loops once and keeps a running count for each item.

1def count_items(items):
2 counts = {}
3 for item in items:
4 counts[item] += 1
5 return counts