Project Level 1: Beginner
This project is designed for beginner learners who are still learning and practicing Python fundamentals.
Project Description
Your task for today is simple. You should make a program that takes a list of tree species found in a forest and counts how many times each species appears.
trees = ["oak", "pine", "maple", "oak", "birch", "pine", "oak"]
Expected Output
Here is what your program should print out:
Learning Benefits
List Processing: Work with lists containing real-world data.
Dictionary Handling: Understand how to store and retrieve counts dynamically.
Prerequisites
Required Libraries: No libraries are needed for this project.
Required Files: No files are needed for this project.
IDE: You can use any IDE on your computer to code the project.
Danger Zone
We provide two solutions for this problem. Find them both in the button below:
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}')