a = list([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
b = list([1, 3, 6, 9])
How do I count the number of times an item in list be occurs in list a?
The above example should return a value of 4.
Whilst writing this question, I thought of the following (which appears to work)
a = list([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
b = list([1, 3, 6, 9])
c = 0
for n in b:
if n in a:
c += 1
continue
print (c)
But there must be a neater way using list comparisons or something?
You can use built-in sum
:
sum(i in b for i in a)
Output:
4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With