1 Comment

My two solutions are slightly different to the solutions given:

Rather than using counter I used count() and set() when looping through the list like:

trees = ["oak", "pine", "maple", "oak", "birch", "pine", "oak"]

for tree in set(trees):

print(f'{tree}: {trees.count(tree)}')

which gave the correct answer but in a different order.

I also used a dictionary method:

trees = ["oak", "pine", "maple", "oak", "birch", "pine", "oak"]

tree_dict = {}

for tree in trees:

tree_dict[tree] = trees.count(tree)

for tree, qty in tree_dict.items():

print(f'{tree}: {qty}')

Expand full comment