Editor

Count Element

Lists · Beginner · 40 XP · about 4 min

Write a function called count_element(lst, element) that counts how many times an element appears in a list.

Example

count_element([1, 2, 2, 3, 2], 2)    # Returns 3
count_element(["a", "b", "a"], "a")  # Returns 2
count_element([1, 2, 3], 5)          # Returns 0

Requirements

  • Return the count as an integer
  • Return 0 if element is not found
  • Handle both numbers and strings
def count_element(lst, element):
# Your code here
pass
# Test your function
print(count_element([1, 2, 2, 3, 2], 2))
print(count_element(["a", "b", "a"], "a"))

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.