DDC-7: Count the 7's
A data challenge a day helps you master machine learning
About these daily data challenges
Each post is an exercise that helps you learn about data in Python.
Try to solve the exercise before checking my solution at the bottom of the post 🤓
You can share your solution or visualization in the comments!
Today’s challenge
Write code that counts the number of times “7” appears in the following list:
alist = [ 1,3,7,5,7,9,2,7,4,7,6,8 ]
Come up with two solutions, one using a method on a list object, and one using list comprehension.
.
.
.
.
Scroll down for the solution…
.
.
.
.
.
.
.
.
keep scrolling!
.
.
.
.
alist = [1,3,7,5,7,9,2,7,4,7,6,8]
print(alist.count(7))
print(len([x for x in alist if x==7]))