0

I am beginner and have a question; is there a faster way to get amount of specific element in list?

a = [False, True, False, True, True]
e = []
for i in range(0, len(a)):
    if a[i] == True:
        e.append(a[i])
print(len(e))
Ignesus
  • 33
  • 3
  • 3
    Well in this specific case you could just use `sum(a)` – user8408080 Dec 23 '19 at 21:17
  • Does this answer your question? [How can I count the occurrences of a list item?](https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item) – AMC Dec 23 '19 at 21:52
  • This is a duplicate, had you done any research? – AMC Dec 23 '19 at 21:52

1 Answers1

1

Yes: the count method.

print(a.count(True))
chepner
  • 497,756
  • 71
  • 530
  • 681