python find count of certain value in list code example

Example 1: python count matching elements in a list

# Basic syntax:
sum(1 for item in your_list if item == "some_condition")
# This counts the items in your_list for which item == "some_condition"
#	is true. Of course, this can be changed to any conditional statement

Example 2: python count value in list

from collections import Counterli = ['blue', 'pink', 'green', 'green', 'yellow', 'pink', 'orange']Counter(li)#=> Counter({'blue': 1, 'pink': 2, 'green': 2, 'yellow': 1, 'orange': 1})

Tags:

Misc Example